0

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
1

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
  • Don't use cat like this, it's dangerous and also introduces additional needless redirect. Use <"file.f" while read d; do.... – orion Jan 21 '15 at 10:44
0

You can use:

$ var="/home/durrantm/a/b/c"
$ cd $var
$ pwd
/home/durrantm/a/b/c
  • I think the idea is that there can be a list of directory names in file.f – rghome Jan 21 '15 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.