BASH Shell Frequently Asked Questions

This document contains a set of frequently-asked questions concerning Bash, the GNU Bourne-Again Shell. Bash is a free and default command interpreter with advanced features for both interactive use and shell programming under Linux. Also don't forget TLDP bash guide / Advanced Bash-Scripting Guide.

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 2 comments… read them below or add one }

1 Manojg 03.23.08 at 4:04 pm

Hi,

I am using bash shell. I have a list of selected directories and want to loop over these directories. The code is like this:

=========================================
#!/bin/bash
dir1=/home/abc/xyz1
dir2=/home/abc/xyz2
dir3=/home/cft/dsf
————
————

x=1
dir=”"
while [ $x -le 8 ]; do
echo $(”dir”$x)
let x++
done
===========================================

In output it produces dir1, dir2, dir3 etc., not the full name directory as defined above. How should I do this?

Thanks
Manojg

2 Zhiyi 04.16.08 at 9:48 pm

See my solution

#!/bin/bash

#one approach is to use array
dir=(/home/abc/xyz1 /home/abc/xyz2 /home/cft/dsf)
x=0
while [ $x -lt ${#dir[@]} ];
do
echo ${dir[x]}
((x++))
done

# another one is just use 'for'
echo ""
dirs="/home/abc/xyz1 /home/abc/xyz2 /home/cft/dsf"
for dir in $dirs
do
echo $dir
done

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , ,

Previous post: Mirror directory with mirrordir command

Next post: CentOS 5 Apache 2.2.3 files failing to download or corrupted download file issue