I want write a function which changes the working directory by passing the name of a directory variable which has already been established like :
foo () { cd $"$1"
}However when I try , for example, foo links I get -bash: cd: links: No such file or directory
Is it possible to do this ?
11 Answer
You can use variable indirection:
foo () { cd "${!1}"
}
dir=/mnt
foo dir
pwd # /mnt