10

Is there way of inserting a quote at the start and end into a string variable?

I have this variable say:

string="desktop/first folder"

If I echo this, it will display:

desktop/first folder

I need the string to be stored as:

"desktop/first folder"
Ali Gul
  • 103

3 Answers3

14

In bash you can use \ as an escape character what whatever follows it. In your case, use it like this:

string="\"desktop/first folder\""
undo
  • 6,129
jcbermu
  • 17,822
8

If you don't do variable substitution, using single quotes as delimiters, so you can use double quotes in your string:

string='"desktop/first folder"'
xenoid
  • 10,597
1

In the case one needs to do variable substitution:

string='"'"$folder"'"'

The inner double quotes allow variable expansion, and each of the outer double quote is flanked by single quotes to "keep" them at their place.

jorvaor
  • 21