BASH tr command
Asked Answered
R

5

5

Id like to convert it to uppercase for the simple purpose of formatting so it will adhere to a future case statement. As I thought case statements are case sensitive.

I see all over the place the tr command used in concert with echo commands to give you immediate results such as:

echo "Enter in Location (i.e. SDD-134)"
read answer # user enters "cfg"

echo $answer | tr '[:lower:]' '[:upper:]' # which produced

cfg # first echo not upper?

echo $answer #echo it again and it is now upper...

CFG
R answered 22/6, 2012 at 15:13 Comment(6)
I fail to see how this code could ever change $answer - you don't assign the output of tr back to $answer, so $answer should STAY cfg.Baines
first echo is upper for me. second is notVanesavanessa
Did you have a question?Anchoress
I see what I did wrong and also I now understand the above and below answers given. I mistakenly had a test line of code later in the script. THANKS for the assistance =)R
What is with the Down Vote? I only turned to SO when I found multiple websites and forums with the wrong direction and use of tr that I did not want.R
You're being downvoted because the first sentence in your question is not at all clear. What is it that you're hoping to do, and what is the problem?Squinty
T
23

This version doesn't require bash, but uses a pipe:

read -p "Enter in Location (i.e. SDD-134) " answer
answer=$(echo "$answer" | tr '[:lower:]' '[:upper:]')
echo "$answer"

And if you're using bash and don't care about portability you can replace the second line with this:

answer="${answer^^}"

Check the "Parameter Expansion" section of bash's man page for details.

Tropaeolin answered 22/6, 2012 at 15:19 Comment(0)
P
7

Echoing a variable through tr will output the value, it won't change the value of the variable:

answer='cfg'
echo $answer | tr '[:lower:]' '[:upper:]'
# outputs uppercase but $answer is still lowercase

You need to reassign the variable if you want to refer to it later:

answer='cfg'
answer=$(echo $answer | tr '[:lower:]' '[:upper:]')
echo $answer
# $answer is now uppercase
Pogge answered 22/6, 2012 at 15:17 Comment(2)
so the echo after: answer$(echo <---- does not echo in the shell?R
Correct, it does not get output. It gets captured and assigned back to the variable.Pogge
V
4

In bash version 4 or greater:

answer=${answer^^*}
Vernal answered 22/6, 2012 at 16:53 Comment(0)
F
0

It is not clear what you are asking, but if you are trying to convert the user input to uppercase, just do:

sed 1q | tr '[:lower:]' '[:upper:]' | read answer

In shells that do not run the read in a subshell (eg zsh), this will work directly. To do this in bash, you need to do something like:

printf "Enter in Location (i.e. SDD-134): "
sed 1q | tr '[:lower:]' '[:upper:]' | { read answer; echo $answer; }

After the subshell closes, answer is an unset variable.

Fieldstone answered 22/6, 2012 at 15:19 Comment(1)
In bash you could just say read answer < <(tr '[:lower:]' '[:upper:]'<<<"$answer") and avoid the pipe that way.Endive
M
-4

good and clear way to uppercase variable is

$var=`echo $var|tr '[:lower:]' '[:upper:]'`

Note Bene a back quotes

Makeup answered 22/6, 2012 at 16:38 Comment(1)
This is not valid syntax for assignment; you should omit the first $Endive

© 2022 - 2024 — McMap. All rights reserved.