Bash – How to Uppercase Command Line Arguments

bashcase sensitivitystring

I searched SO and found that to uppercase a string following would work

str="Some string"
echo ${str^^}

But I tried to do a similar thing on a command-line argument, which gave me the following error

Tried

#!/bin/bash
             ## Output
echo ${1^^}  ## line 3: ${1^^}: bad substitution
echo {$1^^}  ## No error, but output was still smaller case i.e. no effect

How could we do this?

Best Answer

The syntax str^^ which you are trying is available from Bash 4.0 and above. Perhaps yours is an older version (or you ran the script with sh explicitly):

Try this:

str="Some string"
printf '%s\n' "$str" | awk '{ print toupper($0) }'