0

I have below shell script:

while [[ -z "$hosts" ]];do
read hosts?"some string"
done

I want to convert this string to uppercase:

echo $hosts | awk '{print toupper(0)}'

Can I use this converted variable on some way? something like this:

bighosts="echo $hosts | awk '{print toupper(0)}'"

echo $bighosts

I know its not working, can you please provide me some way to use converted variable inside the script instead of creating new text file?

1
  • 1
    please update your posting and specify which shell. bash has built-in case conversions Commented Jul 28, 2020 at 12:59

1 Answer 1

0

Here are two ways to convert a variable to upper case, one using awk and the other using tr:

host=abcd

# using awk
bighost=$(echo $host | awk '{print toupper($0)}')
echo host=$host bighost=$bighost
host=abcd bighost=ABCD

# using tr
bighost2=$(echo $host | tr 'a-z' 'A-Z')
echo host=$host bighost2=$bighost2
host=abcd bighost2=ABCD
Sign up to request clarification or add additional context in comments.

3 Comments

Why not show the bash built-in way?
Also, you have bugs here if you don't quote your parameter expansions. See I just assigned a variable, but echo $variable shows something else! and/or BashPitfalls #14
Also, note the "Answer Well-Asked Questions" section of How to Answer, particularly including the bullet point regarding questions that "have been asked and answered many times before".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.