Skip to content
Advertisement

zsh parameter expansion syntax: combining default value and conversion to upper case

In a zsh script,

JavaScript

outputs the value of the variable X, or 4711 if there is none.

JavaScript

outputs the value of the variable X, converted to upper case.

I wonder, whether there is a way to combine the two, i.e. to have the effect of

JavaScript

without introducing an auxiliary variable.

Advertisement

Answer

JavaScript

From man zshexpn:

If  a  `${...}`  type  parameter expression or a `$(...)` type command
substitution is used in place of name above, it is expanded first and
the result is used as if it were the value of name.  Thus it is possible
to perform nested operations:  `${${foo#head}%tail}` substitutes the value
of `$foo` with both 'head' and 'tail' deleted.
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement