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 host1.my.com,host2.my.com,...host10.my.com then I can use brace expansion at shell prompt as follows:
$ echo host{1..5}.my.comhost1.my.com host2.my.com host3.my.com host4.my.com host5.my.com
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 ;)
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop











{ 2 comments… read them below or add one }
albeit simple, thats pretty sweet. I didnt know how to do that :D
I am trying to understand a shell script demo1.sh
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 demo1.sh *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?