Bash – Use Parameter Expansion to Modify Output of Another Expansion

bash

I'm trying to use the Bash parameter expansions to modify the output of a command substitution or another parameter expansion.

The following nested expansions work quite well in Zsh; but result in a "bad substitution" error in Bash:

${${PWD##*/}//trunk/latest}

or

${$(basename $PWD)//trunk/latest}

the output should be the last folder of the $PWD, replaced by latest when my current directory is trunk

so /home/user/trunk should become latest

Is there a Bash equivalent allowing to chain expansions without relying on variables or pipes? Or do Bash expansions only allows the input to be a string or a plain variable?

Best Answer

No, that nesting of substitution operators is unique to zsh.

Note that with zsh like with (t)csh, you can also do ${PWD:t:s/trunk/latest/}.

Though bash also supports those csh history modifiers for history expansion, it doesn't support them for its parameter expansions.

Here with bash, use a temporary variable:

var=${PWD##*/} var=${var//trunk/latest}
Related Question