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.

3 Answers 3

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.

1
5

With tr command:

Script:

#!/bin/bash

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

Check:

$ bash myscript.sh abc 123 abc
ABC 123 ABC
0

typeset -u VAR=VALUE works too

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .