[Bash] quotes

Zhentiw發表於2024-06-10

Single quotes

If you want to use characters like < or > in the arguments to a program, you will need to use quotes so that the shell doesn't try to interpret them.

For example, to echo the string <b>wow</b> we can use single quotes:

~ $ echo '<b>wow</b>'
<b>wow</b>

Single quotes= print as it is

Double quotes

Double quotes are similar but environment variables and backticks will be interpolated in-place (replaced with their value):

~ $ echo "There's no place like $HOME."
There's no place like /home/substack.
~ $ echo "So long `date +%Y`..."
So long 2014...
~ $ echo "So long `date +%Y`... next stop $((`date +%Y`+1))"'!'
So long 2014... next stop 2015!

You will also need to use quotes if one of the arguments you want to give has a whitespace character in it, because whitespace is otherwise used to split arguments.