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?

James Z
  • 12,209
  • 10
  • 24
  • 44
davi
  • 1
  • 1
    please update your posting and specify which shell. bash has built-in case conversions – Milag Jul 28 '20 at 12:59

1 Answers1

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
LeadingEdger
  • 604
  • 4
  • 7
  • Why not show the bash built-in way? – Charles Duffy Aug 01 '20 at 23:55
  • 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!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) and/or [BashPitfalls #14](https://mywiki.wooledge.org/BashPitfalls#echo_.24foo) – Charles Duffy Aug 01 '20 at 23:56
  • Also, note the "Answer Well-Asked Questions" section of [How to Answer](https://stackoverflow.com/help/how-to-answer), particularly including the bullet point regarding questions that "have been asked and answered many times before". – Charles Duffy Aug 02 '20 at 00:03