How do I use the bash C style for loop under UNIX or Linux operating systems?
The bash C-style for loop share a common heritage with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression (EXP3). The syntax is as follows:
for (( EXP1; EXP2; EXP3 )) do shell-command-1 shell-command-2 done
Bash C-Style Example
#!/bin/bash # Display message 5 times for ((i = 0 ; i < 5 ; i++)); do echo "Welcome $i times." done
Sample outputs:
Welcome 0 times. Welcome 1 times. Welcome 2 times. Welcome 3 times. Welcome 4 times.
Read An Array Using C Style For Loop
Bash provides one-dimensional array variables. An array is created automatically using the following compound assignments syntax:
array=( item1 item2 item3 ... itemN)
You can read an array using for loop as follows:
#!/bin/bash # define an array called fruits fruits=("Apple" "Mango" "Pineapple" "Banana" "Orange" "Papaya" "Watermelon") len=${#fruits[*]} # get total elements in an array # print it for (( i=0; i<${len}; i++ )); do echo "${fruits[$i]}" done
Here is another practical example that generates lighttpd web server configuration file to log real IP address of the visitors:
#!/bin/bash _frontend_proxy_lan_ips=("10.10.29.72" "10.10.29.71" "10.10.29.70" "10.10.29.69" "10.10.29.68") t="/tmp/lighttpd.backend.conf.$$" at=${#_frontend_proxy_lan_ips[*]} # get total elements in an array s="" echo '### Log real client ips on all backends ###' >"$t" echo 'server.modules += ( "mod_extforward" )' >>"$t" echo 'extforward.headers = ("X-Forwarded-For")' >>"$t" echo 'extforward.forwarder = (' >>"$t" # For loop for (( i=0; i<${at}; i++ )); do [ $i -lt $(( $at - 1 )) ] && s="," || s="" # remove , for last item in an array echo " \"${_frontend_proxy_lan_ips[$i]}\" => \"trust\"${s} " >>"$t" done echo ')' >>"$t" # Copy it cp -f "$t" /etc/lighttpd/ # remove temp file [ -f "$t" ] && rm -f "$t"
Sample outputs (sample config file generated by above script):
### Log real client ips on all backends ### server.modules += ( "mod_extforward" ) extforward.headers = ("X-Forwarded-For") extforward.forwarder = ( "10.10.29.72" => "trust", "10.10.29.71" => "trust", "10.10.29.70" => "trust", "10.10.29.69" => "trust", "10.10.29.68" => "trust" )
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 }
Hi,
I have just stumbled upon this site and like it a lot. Yet when reading Bash I was immediately thinking about Zsh and that I should have switched from to Zsh rather earlier than later. I would hardly put any effort in Bash and spent the time on Zsh instead.
Keep up the good work.
More simple example of array
GNU nano 2.2.6 File: a Modified
#!/bin/bash fruits=("Apple" "Mango" "Pineapple" "Banana" "Orange" "Papaya" "Watermelon") for I in ${fruits[*]}; do echo $I done