5

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

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
Veljko
  • 1,708
  • 12
  • 40
  • 80

1 Answers1

16

Escaping quote sign with backslash is what you need

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

Additionally, curly braces is safe notation for variables

Tag Wint
  • 407
  • 3
  • 9
  • works great. Thank you!!! And one more question please: if I want to have maybe as result this: 'Boston' what do I need to change? Thanks. I will accept answer in few minutes – Veljko Nov 23 '18 at 11:03
  • you need to change just one sign single quote is escaped the same way as the double one: \" -> \' – Tag Wint Nov 23 '18 at 11:05
  • Downvote: *Not* quoting the argument to `echo` makes you do all kinds of weird complications which could be solved very easily. – tripleee Nov 23 '18 at 11:45
  • `function stpu() { git stash push -u -m \""${@}"\" }` is not working for me. – Ryan Nov 13 '20 at 15:30