Bash – Convert arg to uppercase to pass as variable

bashcshshell

Is there a way to convert the command line argument to uppercase and pass it as a variable within the script being invoked?

Eg. ./deploy_app.csh 1.2.3.4 middleware 

should convert middleware to MIDDLEWARE and pass it as a variable inside the script where ever it requires a variable substitution.

I know that I can use echo and awk to get this output but trying to check if there is a way without using that combination

Best Answer

Using bash (4.0+), inside the script:

newvarname=${3^^}

Using tcsh:

set newvarname = $3:u:q

Using zsh:

# tcsh-like syntax:
newvarname=${3:u} # or just $3:u
# native syntax:
newvarname=${(U)3}

Using tr instead of shell features (though limited to single-byte letters only in some tr implementations like GNU's):

newvarname=$(printf "%s" "$3" | tr '[:lower:]' '[:upper:]')

This page summarizes a lot of features of different UNIX shells, including text manipulation: http://hyperpolyglot.org/unix-shells.

Related Question