Is it possible to perform more than one bash variable manipulation in one shot?
For example:
# for this variable
foo="string_that_is_lower"
# can I do this in one shot?
$foo="${foo:0:4}"
echo "${foo^^}"
Is there a way to combine these? I realize this is a trivial problem because this works just fine, or one could simply use other built in tools like echo ${foo:0:4} | tr [[:lower:]] [[:upper:]]
, but that is lame.
I've tried every logical combination I can think might work:
${${foo^^}:0:4}
${{foo^^}:0:4}
${foo^^,:0:4}
${foo^^;:0:4}
${foo^^ :0:4}
All produce syntax errors.
I find no instances of "(combine|multiple) bash variable manipulations" in the Advanced Bash Scripting Manual, or the manpage, or Google, or here, so maybe you just can't do it, but probably I'm just not searching for the right terms.
It is a very good question, but not something that parameter expansion allows. man bash
provides that expansion operates on a parameter name or symbol, e.g.
Parameter Expansion
The `$' character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as partof the name.
When you seek to chain expansions together, you are attempting to use the text that results from the first expansion as a parameter name or symbol (which it isn't), so the command fails with a syntax error.
Good question!
(note: to be complete -- there are some instances where you can embed a parameter expansion within another parameter expansion, but those are limited to where the outer expansion is looking for a single number as part of its expansion which can be provided by an included expansion such as the length of a string, e.g. ${#var}
in v=foo; echo ${v:$((${#v}-1))}
)
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