I have a problem with variable substitution in bash. I'm doing like as explained in below code, but cd $d is not going into original directory i.e. after expanding $var
export var="/a/b/c"
for d \`cat file.f`; do
cd $d
done
file.f:
$var/aa/bb/cc
You need to use eval:
export var="/a/b/c"
for d in $(cat file.f)
do
eval echo "$d"
eval cd $d
pwd
done
or better still, using read:
export var="/a/b/c"
while read d
do
eval echo "$d"
eval cd "$d"
pwd
done < file.f
cat like this, it's dangerous and also introduces additional needless redirect. Use <"file.f" while read d; do....
You can use:
$ var="/home/durrantm/a/b/c"
$ cd $var
$ pwd
/home/durrantm/a/b/c