Search code examples
bashshellvariablesdouble-quotes

SHELL add double quotes at the beginning and end of specific variable


I have variable in shell script which takes a value in variable $var1. If I do echo $var1 I will get for example value Boston. My desired value is "Boston"

I tried couple of cases:

var1=""$var1""
var1="""$var1"""

But in both cases I am getting as result '$var1' but I want "Boston" Please for solution do not mention solution which contains Boston in it. I would appreciate if you can use only var1 in the whole script so it can be more clear! "Boston" just needs to be output of the script execution and for initiliztion for the variable var1.

Thanks


Solution

  • Escaping quote sign with backslash is what you need

    var1=Boston
    var1=\"${var1}\"
    echo $var1
    "Boston"
    

    Additionally, curly braces is safe notation for variables