Softpanorama

Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
May the source be with you, but remember the KISS principle ;-)
Skepticism and critical thinking is not panacea, but can help to understand the world better

Brace Expansion

News

Bash Variables

Recommended  Links

Process Substitution in Shell Subshells
ksh completion Bash completion modes bash programmable completion package Command history reuse cdpath
Sysadmin Horror Stories Unix shells history Tips Humor Etc

Everyone has done one of the following to make a quick backup of a file test:

cp test test.old  /tmp
cp test.old test  /tmp 

But retyping or even copying name if it is long is a inconvenient and time consuming. As Unix came from world of teletypes with their slow speed, and duringthis perios programmers invented very sophisticated ways to cut number of stokes, as teletypes were not very reliable.  Factoring of common part of the filename using { } blakets is remant from this ancient period. For example to copy  twp files -- test and test.old  -- you can write:

 cp test{, ".old"} /tmp
 cp test{".old",}  /tmp  

These two commands are doing exactly the same thing as the first two, but with less typing.   The curly brace ("{") in this context means  "brace expansion". The preamble (in our case test,) is prepended to each of the strings in the comma-separated list found within the curly braces, creating a new word for each string.

Brace expansion can take place anywhere in your command string, can occur multiple times in a line and can be nested. Brace expansion expressions are evaluated left to right. Some examples:

touch a{1,2,3}_sample
ls /usr/{,local/}
mkdir -p /db/vendors/{dell,hp,cisco}

Brace expansion can be nested.

 touch a{1,2,3{,orig}}

The shell will expand it to:

 a1 a2 a3 a3orig
You can use ranges instead of sequence
echo {a..f}{1..9}.txt
According to bash man page

"A sequence expression takes the form {x..y}, where x and y are either integers or single characters. When integers are supplied, the expression expands to each number between x and y, inclusive. When characters are supplied, the expression expands to each character lexicographically between x and y, inclusive. Note that both x and y must be of the same type".


Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

Bash Brace Expansion By Mitch Frazier

May 30, 2008 | Linux Journal

Bash brace expansion is used to generate stings at the command line or in a shell script. The syntax for brace expansion consists of either a sequence specification or a comma separated list of items inside curly braces "{}". A sequence consists of a starting and ending item separated by two periods "..".

Some examples and what they expand to:

  {aa,bb,cc,dd}  => aa bb cc dd
  {0..12}        => 0 1 2 3 4 5 6 7 8 9 10 11 12
  {3..-2}        => 3 2 1 0 -1 -2
  {a..g}         => a b c d e f g
  {g..a}         => g f e d c b a
If the brace expansion has a prefix or suffix string then those strings are included in the expansion:
  a{0..3}b       => a0b a1b a2b a3b
Brace expansions can be nested:
  {a,b{1..3},c}  => a b1 b2 b3 c

Counted loops in bash can be implemented a number of ways without brace expansion:

# Three expression for loop:
for (( i = 0; i < 20; i++ ))
do
    echo $i
done
# While loop:
i=0
while [[ $i -lt 20 ]]
do
    echo $i
    let i++
done
# For loop using seq:
for i in $(seq 0 19)
do
    echo $i
done
A counted for loop using bash sequences requires the least amount of typing:
for i in {0..19}
do
    echo $i
done
But beyond counted for loops, brace expansion is the only way to create a loop with non-numeric "indexes":
for i in {a..z}
do
    echo $i
done

Brace expansion can also be useful when passing multiple long pathnames to a command. Instead of typing:

  # rm /a/long/path/foo /a/long/path/bar
You can simply type:
  # rm /a/long/path/{foo,bar}

Brace expansion is enabled via the "set -B" command and the "-B" command line option to the shell and disabled via "set +B" and "+B" on the command line.

Bash Brace Expansion Tutorial 6 Examples of Expanding Expressions within Braces

If you see the output of the following two for statement, you could identify the above pitfall.

$ cat var_seq.sh
# Print 1 to 4 using sequences.
for i in {1..4}
do
        echo $i
done
start=1
end=4

# Print 1 to 4 using through variables
echo "Sequences expressed using variables"
for i in {$start..$end}
do
        echo $i
done

$ ./var_seq.sh
1
2
3
4
Sequences expressed using variables
{1..4}
Fun with bash shell brace expansion " the semi-crazy blog
Brace expansion only happens once, right after the command line is tokenized. So this works:
$ echo foo{1,2,3}
foo1 foo2 foo3

That's great, but this does not work:

$ myline=foo{1,2,3}
$ echo $myline
foo{1,2,3}

This does not work since the brace expansion (foo{1,2,3} -> foo1 foo2 foo3) happens prior to the shell parameter expansion ($myline -> foo{1,2,3}). To examplify the order, try the reverse experiment. It should work out the same way:

$ one=1
$ two=2
$ echo values_{$one,$two}
values_1 values_2

So it makes sense, but if we still want to force our $myline variable value through brace expansion, we'll need to have bash evaluate it twice. for this, we'll need the bash builtin 'eval' command. eval evaluates a command line for you, and using it with echo and backtics you can get it to double evaluate our variable, like this:

$ myline=foo{1,2,3}
$ evaluatedline=`eval echo $myline`
$ echo $evaluatedline
foo1 foo2 foo3

And there you go. Now, using a similar mechanism, we can iterate through our file based list of brace expressions, expand each one and copy the files I want to a new location and sequenced name. The full script to do this is here:

#!/bin/bash
OUTPUT_DIR=./quicktime/qt_input_files
OUTPUT_FILE_PREFIX=p
OUTPUT_FILE_SUFFIX=.jpg
FILE_OF_PATTERNS=file-list.txt

# make the output directory if it doesn't exist
if [ ! -e $OUTPUT_DIR ]
then
 mkdir -p $OUTPUT_DIR
fi

# clear out previous run's output
# use this construct since passing such a huge amount (12,000) of
#   files to rm (via rm *.jpg)
#    will fail otherwise (due to 'too many args' error)
ls -tr $OUTPUT_DIR | xargs -t -I{} rm -f ${OUTPUT_DIR}/{}

COUNT=1
# $FILE_OF_PATTERNS is the path to a file that contains path specifiers,
#   one per line, like this:
# 2004-10-26/cam_2004-10-26_{08,09_00}*.jpg
# each line specifies some files we want to include in the animation
# do this for each pattern spec I want to grab
for i in `egrep -o '^[^#]*'  $FILE_OF_PATTERNS`
do
 echo =========== PROCESSING: $i ================
 # force the brace expression through another evaluation, like so:
 foo=`eval echo $i`
 # for every file that matches this sub pattern, copy that image
 #   to a file named with a more simple sequence (p1.jpg, p2.jpg, ...)
 for file in `ls $foo 2> /dev/null`
 do
   cp -p ${file} ${OUTPUT_DIR}/${OUTPUT_FILE_PREFIX}${COUNT}${OUTPUT_FILE_SUFFIX}
   let COUNT=$COUNT+1
 done
done
...

Shell scripting and brace expansion

Now try it in a shell script:

#!/bin/bash
HOSTS="$1"
for i in $HOSTS
do
ping $i
# rest of logic
done

And then executed script by typing command:

$ ./myscript host{1..5}.my.com 

It will not expand to host1.my.com, host2.my.com..... :/? It took me more than two hours, finally while chatting with my friend he told me to replace HOSTS="$1" with HOSTS="$@". Bingo it worked!

According to bash man page,"A sequence expression takes the form {x..y}, where x and y are either integers or single characters. When integers are supplied, the expression expands to each number between x and y, inclusive. When characters are supplied, the expression expands to each character lexicographically between x and y, inclusive. Note that both x and y must be of the same type". $@ is a special shell variable which. expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. I must admit I need to master shell shell scripting skills ;)

Recommended Links

Google matched content

Softpanorama Recommended

Top articles

Sites



Etc

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright 1996-2018 by Dr. Nikolai Bezroukov. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) in the author free time and without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down you can use the at softpanorama.info

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March 12, 2019