BASH: How can I combine two (or more) variable string manipulations in one step?
You could do this in bash
if you use read
to read two variables:
$ echo "$XDG_CURRENT_DESKTOP"
ubuntu:GNOME
$ IFS=':' read var1 var2 <<<"${XDG_CURRENT_DESKTOP@L}"
$ echo "$var2"
gnome
The ${variable@L}
construct returns the value of $variable
converted to lower case. Then, IFS=':' read
sets the input field separator to :
for the read
command, this way the global IFS is left unchanged after the command exits, and then read var1 var2
will separate its input on :
and save the result in the two variables var1
and var2
. Note that if you have more than one :
on the same line in the input, var1
will have everything up to the first :
and var2
everything else.
Finally, <<<
is a "here string", a simple way to pass a variable as input to a command.