Score:0

Is it possible to write a bash function to change directory by passing the name of a directory variable?

kr flag

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 ?

hr flag
See [How do I assign a variable in Bash whose name is expanded ($) from another variable?](https://askubuntu.com/questions/926450/how-do-i-assign-a-variable-in-bash-whose-name-is-expanded-from-another-varia)
Score:3
tm flag

You can use variable indirection:

foo () {
    cd "${!1}"
}

dir=/mnt
foo dir
pwd  # /mnt
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.