Shell scripting and brace expansion

Expansion is performed on the command line after it has been split into words. Brace expansion is a mechanism by which arbitrary strings may be generated. A sequence expression takes the form {x..y}, where x and y are either integers or single characters. Simple bash brace expansion example:

$ echo F{1,2,3,4,5}
F1 F2 F3 F4 F5

It works with almost any command:

$ mkdir -p /home/project/{sales,purchase,reports}

It is funny but some time you can stuck in shell scripting very badly and you do not understand what is going on … For example I need to expand hostnames using,,… then I can use brace expansion at shell prompt as follows:

$ echo host{1..5}

Now try it in a shell script:

for i in $HOSTS
ping $i
# rest of logic

And then executed script by typing command:

$ ./myscript host{1..5} 

It will not expand to,….. :/? 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 😉

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • riscphree Dec 18, 2005 @ 15:10

    albeit simple, thats pretty sweet. I didnt know how to do that 😀

  • abhishek Mar 7, 2013 @ 5:54

    I am trying to understand a shell script
    this is used to count the number of files in a directory

    declare -a args=( “$@” )
    echo ${#args[@]} # count the elements of the array args

    bash *pdf will give you the correct number of pdf files.

    Can you explain how does this work or how is it getting expanded specially in first line
    (“$@”) why the bracket () is used here and in second line args[@] what does this gets expanded to?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum