0

There are 3 values for the variable City as my input - New York, Mumbai, London When I try to execute echo ${City} in bash, i get

New York,Mumbai,London.

But I require to get the above list within double quotes, like :

"New York,Mumbai,London"

Is there a possible way ?

Toto
  • 89,455
  • 62
  • 89
  • 125
  • 2
    Do you want to store the double-quotes as part of the variable's value, or only add them when printing it? In either case, you should also put double-quotes around any variable when you use it (that is, use `echo "${City}"` instead of just `echo "${City}"`). These quotes will not be treated as part of the data, they just tell the shell not to do anything strange with the variable's data. See [this question](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable). And use [shellcheck.net](https://www.shellcheck.net) to spot common problems. – Gordon Davisson Jun 14 '21 at 09:07
  • Thanks for the reply. And yes, the double quotes is not part of value. The values had spaces in between, so had to pass as comma separated strings, and due to some other requirements, this list had to be passed within double quotes. – the_nub_kodar Jun 14 '21 at 09:19

2 Answers2

2

You can either wrap quote whenever creating variable or you can modify existing variable like below

Here is example :

#!/bin/bash
# GNU bash, version 4.4.20

#  your existing variable whatever
city="New York,Mumbai,London"

# wrap quote
city=\"$city\"

# echo variable
echo "$city"

Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
1

Just put your desired outputs in a row

echo '"'${City}'"'
Dri372
  • 1,275
  • 3
  • 13
  • 1
    While this prints double-quotes around the string, it doesn't have syntactic double-quotes to protect against unexpected parsing of the variable's value. Use `echo "\"${City}\""` or `echo '"'"${City}"'"'` instead. – Gordon Davisson Jun 14 '21 at 09:21