How to pass the value of a variable to uppercase or minuscule?

Asked

Viewed 338 times

13

I have a script that gets a variable when running, for example:

./myscript OLA

the content of myscript:

#!/bin/bash

if [ -z $1 ]
then
  echo "Por favor escolha uma opção:"
  echo "OLA - Faz qualquer coisa"
  echo "OLE - Faz outra coisa"
  ...
else
  echo "Vou fazer alguma coisa consoante a variável."
  if [ $1 = "OLA" ]
  then
    echo "Olá amigo."
    ...

I want to ./myscript ola or ./myscript OlA, etc also work. How can I pass the $1 uppercase for comparison? By the way and for minuscula?

  • 5

    https://stackoverflow.com/questions/2264428/how-to-convert-a-string-to-lower-case-in-bash

2 answers

8


As I had mentioned in the comments, there is a shell expansion to do this: ${variable^^} will transform the text into variable high-cash.

I’ll copy the examples of this answer in Stackoverflow international, because it even shows behaviors that I didn’t know:

$ string="a few words"
$ echo "${string^}"
A few words
$ echo "${string^^}"
A FEW WORDS
$ echo "${string^^[aeiou]}"
A fEw wOrds

$ string="A Few Words"
$ declare -u string
$ string=$string; echo "$string"
A FEW WORDS
  • expansion with a single circumflex ${var^} transforms the first letter cashier
  • expansion with two circumflex ${var^^} transforms all letters cashier
  • expansion with an expression blob after the circumflex transforms the characters that match the pattern

On this default expression, note that if you use it with a single circumflex, it will only put it in a high box if the first character matches the expression:

$ var=abcd
$ echo ${var^[bcd]}
abcd
$ echo ${var^^[bcd]}
aBCD
$ echo ${var^[abcd]}
Abcd

You can understand expansion with circumflex a special case of expansion with circumflex + pattern:

$ echo ${var^^?}
ABCD
$ echo ${var^?}
Abcd

The advantage of this alternative to using awk, tr, perl is that you do not start a new process, the whole execution is done "directly" by Bash.


To turn into lowercase, the expansion uses commas instead of the circumflex. Everything else behaves similarly:

$ string="A FEW WORDS"
$ echo "${string,}"
a FEW WORDS
$ echo "${string,,}"
a few words
$ echo "${string,,[aeiou]}"
a FeW WoRDS

$ string="A Few Words"
$ declare -l string
$ string=$string; echo "$string"
a few words

Out of curiosity, did you notice that there is a special variable statement that turns it into a high box or low box in the simple assignment? This is done through the declare of the variable.

In the answer from where I got the examples I found nothing that referenced the why of the pattern of the arguments of declare, but I found this to be extremely noteworthy:

The declare options change the attribute of the variable, but not the Contents. The reassignments in my examples update the Contents to show the changes.

In free translation:

The options of declare change the variable attribute, but not its content. Re-allocations in the examples update the content to show changes.

So why use -u for high cash and -l for low box? If you think in English, it is easier to remember:

  • drop box, lowercase, -l
  • high box, uppercase, -u

Then stay these mnemonics to help in the memorization of flags of variable declaration.

2

You can use awk to do this. Here’s an example. Put the line that converts into your script before you test it. Then test with $VAR instead of $1

   #!/bin/bash
    #$VAR recebe $1 convertido para maiusculas
    VAR=$(echo $1 | awk '{ print toupper($1) }')
    echo "Variavel em maiuscula: $VAR"
  • It is not wrong, but it is not necessary to create a new process to do this.

  • in fact, in shell, it is almost certain that a new process will be created - if not for this, for almost anything - awk may be Overkill, but for other reasons.

  • @jsbueno, there is a variable expansion that does this in Bash, which does not create a new process

  • In the references I’m finding, we use awk '{ print toupper($0) }', nay $1. For example: https://stackoverflow.com/q/14021899/4438007

  • I tested with awk '{ print toupper($0) }' and with awk '{ print toupper($1) }', I got the same results

  • 1

    @Jeffersonquesado - can’t put an answer there with no need for another process? It would be better anyway.

  • @jsbueno, I already put

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.