5

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

Tim Kennedy
  • 19,697

2 Answers2

14

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
8

In Bash you can declare a variable as uppercase with -u, and it then converts automatically.

$ declare -u a
$ b=abcd
$ a=$b
$ echo $a
ABCD
  • 2
    Note that it comes from ksh (where you'd use typeset instead of declare). Also note that it also has the side effect of limiting the scope of the variable to the current function (and the previous value of the variable be restored upon exit of the function). – Stéphane Chazelas Mar 21 '17 at 17:52