Nesting parameter substitutions works in Zsh:
$ param=abc
# nested remove prefix ${...#a} and remove suffix ${...%c} =>
$ printf '%s\n' ${${param#a}%c}
# => b
Is there any equivalent in POSIX?
$ param=abc
$ printf '%s\n' ${${param#a}%c}
# => dash: 2: Bad substitution
# => sh: ${${param#a}%c}: bad substitution
# => bash: ${${param#a}%c}: bad substitution
You can use expr instead to extract the text between the desired prefix and suffix. (This is not, of course, a general purpose equivalent to nested expressions, but does solve your given problem.)
param=abc
expr "$param" : "a\(.*\)c"
The regular expression matching operator : of expr takes two arguments: the left argument is a string, the right argument is a regular expression. The output is whatever is matched inside the \(...\) group.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With