Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

If I can do this in my bash shell:

$ STRING="A String"
$ echo ${STRING^^}
A STRING

How can I change my command line argument to upper case?

I tried:

GUARD=${1^^}

This line produces Bad substitution error for that line.

share|improve this question

3 Answers 3

up vote 7 down vote accepted

Let's start with this test script:

$ cat script.sh 
GUARD=${1^^}
echo $GUARD

This works:

$ bash script.sh abc
ABC

This does not work:

$ sh script.sh abc
script.sh: 1: script.sh: Bad substitution

This is because, on my system, like most debian-like systems, the default shell, /bin/sh, is not bash. To get bash features, one needs to explicitly invoke bash.

The default shell on debian-like systems is dash. It was chosen not because of features but because of speed. It does not support ^^. To see what it supports, read man dash.

share|improve this answer
4  
… and you can "explicitly invoke bash" for a script (i.e., specify that a script should be run by bash) by putting it in the "she-bang" (the first line of the script).  #!/bin/bash will work on many systems, #!/usr/bin/bash will work on others, and #!/usr/bin/env bash should work on all systems that have bash (if it's in your search path).  See Does the shebang determine the shell which runs the script? and Why is it better to use “#!/usr/bin/env NAME” instead of “#!/path/to/NAME” as my shebang? –  Scott Apr 13 at 22:41

typeset -u VAR=VALUE works too

share|improve this answer

With tr command:

Script:

#!/bin/bash

echo $@ | tr '[a-z]' '[A-Z]'

Check:

$ bash myscript.sh abc 123 abc
ABC 123 ABC
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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