I was trying to define an alias that helps me to cd to the directory that is created most recently, and I'm using the following in my .bashrc:alias cdlatest=latestdir=$(ls -td -- */|head -n 1); cd $latestdirBut this alias only works once. If I change to a new directoy, it still tries to cd to the last directory. It seems becaue the variable $(ls -td -- */|head -n 1) is only evaluated once from the start. How to fix this?
Alias accepting variables from the result of evaluation
bash;shell;quoting;alias;command substitution
Change your double quotes around the alias definition to single and youre good to go.alias cdlatest='latestdir=$(ls -td -- */|head -n 1); cd $latestdir'However, I'd suggest a few streamlining:alias cdlatest='cd $(\ls -td -- */|head -n 1)'where we do away the intermediate variable $latestdir, quote the results of the command evaluation $(...), and use an unaliased form of ls.But you need to remember, that this will not handle files with newlines in their names.