0

Had this issue while working with inline bash command and this thing got me really confused. So, say I declare a shell variable temp and give it a value:

> temp='hello'

and now to echo this result in UPPER CASE using inline commands I write

 bash -c "echo ${temp^^}"
 HELLO

which is totally fine and I'm okay with it. Now while studying this topic I came across this line, "Whenever we put code in a string (such as in the case of passing it in as an argument), the code should be single-quoted."

And this line is helpful as it solves the ambiguity can be caused by multiple double quotes.

So, following this I wrote the code as follows:

bash -c 'echo "${temp^^}"'

Which gave me no output. I tried to do a verbose command on this one which gave me this:

bash -cv 'echo "${temp^^}"'
echo "${temp^^}"

Can somebody explain why the result is not getting displayed as HELLO, would really appreciate it.

  • It kinda does, but none of the explanations answered why this was happening? I want to know the reason this happens. Thanks for the remedy for this though :) – Karan Bakshi Mar 07 '21 at 15:05
  • here's another thing that happened. When I try to display the value of positional parameters the result displayed is as expected.example bash -c 'echo "1: $1, 2: $2, 4: $4"' -- 'New First Argument' Second Third 'Fourth Argument' which gives the result: 1: New First Argument, 2: Second, 4: Fourth Argument – Karan Bakshi Mar 07 '21 at 15:11
  • Please note the above referenced answer related to using variables inside single quotes. Since you're starting a new shell, you could export the variable first so that the variable is available in the newly-created process. – Andy Dalton Mar 07 '21 at 17:17
  • @AndyDalton So when we use inline bash, we're essentially carrying out our code in a separate shell, is that right ? – Karan Bakshi Mar 07 '21 at 18:09
  • Try running export temp ;) – cryptarch Mar 07 '21 at 21:33
  • yup, I got it :) – Karan Bakshi Mar 08 '21 at 08:31

0 Answers0