Views:
5,415β
Votes: 5β
β
Solution
Tag :
bash
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1068366
Title:
What is the difference between echo $var and echo "$var"
ID:
/2018/08/23/What-is-the-difference-between-echo-_var-and-echo-__var_
Created:
August 23, 2018
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
There is no difference between echo $var
and echo "$var"
.
However for other commands such as ls
(list files) there could be a big difference.
Try this in your terminal:
$ touch "File A"
$ var="File A"
$ ls $var
ls: cannot access 'File': No such file or directory
ls: cannot access 'A': No such file or directory
$ ls "$var"
File A
The double quotes "
tells Linux to treat everything in between as a single entity. Without the double quotes everything inside is treated as separate entities delineated by spaces.
So in the first example $var
is two different things βFileβ and βAβ.
In the second example "$var"
is one thing "File A"
.
The echo
command automatically processes a single word or multiple words until the end of the line as one thing. Many other commands expect one or many things.