Blog

03/06/2019

Bash foreach loop examples for Linux / Unix



I used foreach on a Unix operating system and csh shell loop. Now, I am a Linux operating system. How do I use foreach loop in bash shell running on Linux?

The C shell (csh) or the improved version, tcsh is a Unix shell from the late 1970s. The csh foreach loop syntax is as follows:

foreach n ( 1 2 3 4 5 )
  #command1
  #command2
end

foreach n ( 1 2 3 4 5 ) #command1 #command2 end


However, bash lacks foreach syntax; instead, you can use bash while loop or bash for loop syntax as described below.

Bash foreach loop examples

Let us say you want to convert the following csh forloop example:

foreach i ( * )
echo "Working $i filename ..."
end

foreach i ( * ) echo "Working $i filename …" end


Above csh or tcsh foreach displayed a list of files using loop. Here is similar code in a bash shell:

for i in *
do
  echo "Working on $i file..."
done

for i in * do echo "Working on $i file…" done


However, one can find and safely handle file names containing newlines, spaces, special characters using the find command

## tested on GNU/Linux find ##
## and bsd/find only ##
find /dir/ -print0 | xargs -r0 command
find /tmp/test -print0 | xargs -I {} -r0 echo "Working on "{}" file ..."
find /tmp/test -type f -print0 | xargs -I {} -r0 echo "Working on '{}' file ..."

## tested on GNU/Linux find ## ## and bsd/find only ## find /dir/ -print0 | xargs -r0 command find /tmp/test -print0 | xargs -I {} -r0 echo "Working on "{}" file …" find /tmp/test -type f -print0 | xargs -I {} -r0 echo "Working on ‘{}’ file …"

Working on '/tmp/test' file ...
Working on '/tmp/test/hosts.deny' file ...
Working on '/tmp/test/My Resume.pdf' file ...
Working on '/tmp/test/hostname' file ...
Working on '/tmp/test/hosts.allow' file ...
Working on '/tmp/test/Another   file     name.txt' file ...
Working on '/tmp/test/resolv.conf' file ...
Working on '/tmp/test/hosts' file ...
Working on '/tmp/test/host.conf' file ...

Howto use foreach in bash shell

Say you have a file named lists.txt as follows:
cat lists.txt
Sample outputs:

/nfs/db1.dat
/nfs/db2.dat
/nfs/share/sales.db
/nfs/share/acct.db 
/nfs/private/users.db
/nfs/private/payroll.db

Each in there is a file located on your Unix or Linux server. Here is how to read lists.txt file and work on each file listed in lists.txt:

for n in $(cat lists.txt )
do
    echo "Working on $n file name now"
    # do something on $n below, say count line numbers
    # wc -l "$n"
done

for n in $(cat lists.txt ) do echo "Working on $n file name now" # do something on $n below, say count line numbers # wc -l "$n" done

Although for loop seems easy to use for reading the file, it has some problems. Don’t try to use “for” to read file line by line in Linux or Unix. Instead, use while loop as follows:

#!/bin/bash
## path to input file 
input="lists.txt"
 
## Let us read a file line-by-line using while loop ##
while IFS= read -r line
do
  printf 'Working on %s file...n' "$line"
done < "$input"

#!/bin/bash ## path to input file input="lists.txt" ## Let us read a file line-by-line using while loop ## while IFS= read -r line do printf ‘Working on %s file…n’ "$line" done < "$input"

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....