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.
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.
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
.
With tr
command:
Script:
#!/bin/bash
echo $@ | tr '[a-z]' '[A-Z]'
Check:
$ bash myscript.sh abc 123 abc
ABC 123 ABC
#!/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 '15 at 22:41