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 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

  • is that a CSH script? You've tagged the "bash" shell in the Q.
    – Jeff Schaller
    Mar 21 '17 at 16:19
  • The script being run is a csh script, but it's possible the shell being used is bash. Mar 21 '17 at 16:26
  • I assume you're right, Tim, but in theory a "foo.csh" script could have some other shell in the she-bang line. Hopefully the OP will clarify.
    – Jeff Schaller
    Mar 21 '17 at 16:27
  • Yes the script being run is a csh but the shell being used for this conversion and variable substitution is bash Mar 22 '17 at 8:23

Using bash (4.0+), inside the script:


Using tcsh:

set newvarname = $3:u:q

Using zsh:

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

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.


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
  • 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). Mar 21 '17 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.