How to print variable's value as literal string

5.9k Views Asked by At

Not sure if the title is precise. Let's say I have a variable with a string:

var=C:\Windows\file.exe

And I'd like to print it's value as if it were a literal string, i.e. I want to see this on the screen:

C:\Windows\file.exe

But, of course, the usual ways to print a variable don't do that:

echo $var
C:Windowsfile.exe
echo "$var"
C:Windowsfile.exe
echo '$var'
$var

Is it possible to do that?

1

There are 1 best solutions below

2
On BEST ANSWER

The problem is that the backslashes are being operated on by shell at definition time, not when you are evaluating the variable later. You need to quote the declaration i.e. use any shell escaping mechanism to escape the \s.

Here is what you are doing:

$ var=C:\Windows\file.exe

$ echo "$var"
C:Windowsfile.exe

Here is what you need:

$ var='C:\Windows\file.exe'

$ echo "$var"
C:\Windows\file.exe