Substitution AND capitalize on ${var}

95 views Asked by At

I have this piece of code :

target='xfce/*'
file_name="All files from ${target/\/*}"`

Later on, I do

echo "Copying [$file_name] folder"

and it shows :

Copying [All files from xfce] folder

I wanted it to show

Copying [All files from XFCE] folder

instead (notice that XFCE is now capitalized)

I know how to capitalize with ${var^^}, but how can do both substitution and capitalization?

I tried

file_name="All files from ${target/\/*^^}"

or

file_name="All files from ${target^^/\/*}"

but none works. Is there a way to achieve substitution AND capitalization in the same command ?

2

There are 2 answers

0
Gilles Quénot On

What I would do:

remplacement=foobar; echo ${var//Pattern/${remplacement^^}}
0
kvantour On

While nested variable expansion is not possible in Bash, in this particular case you could use the following one-liner:

echo $(tr '[:lower:'] '[:upper:]' <<<${var//pattern/repl})