8

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.

Anthon
  • 78,313
  • 42
  • 165
  • 222
user1135541
  • 731
  • 4
  • 9
  • 13

3 Answers3

8

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.

John1024
  • 73,527
  • 11
  • 167
  • 163
  • 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?](http://unix.stackexchange.com/q/87560/23408) and [Why is it better to use “#!/usr/bin/env NAME” instead of “#!/path/to/NAME” as my shebang?](http://unix.stackexchange.com/q/29608/23408) – Scott - Слава Україні Apr 13 '15 at 22:41
5

With tr command:

Script:

#!/bin/bash

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

Check:

$ bash myscript.sh abc 123 abc
ABC 123 ABC
Mandar Shinde
  • 3,156
  • 11
  • 39
  • 58
0

typeset -u VAR=VALUE works too

HalosGhost
  • 4,732
  • 10
  • 33
  • 41