This can get a bit complicated.
Quoting can be used to disable special treatment for special characters,
to prevent reserved words from being recognized as such, and to prevent parameter expansion.
Ordinary strings (without 'special' characters) do NOT need to be quoted.
Quoting is used to remove the special meaning of certain characters or words to the shell.
Quoting in bash is accomplished 4 different ways:
Variables are a special case in bash use.
On the command line (not in a script), the words typed are separated by spaces, tabs, or linebreaks. These are known as whitespace. If a word or command here includes a whitespace character, you have to use single or double quotes to treat it as a single unit. We saw this previously.
However, in a script, double quotes are used to evaluate the contents of a variable.
Single quotes in a script print contents literally.
name="Jeff" echo 'Hello $name' Hello $name
However, double quotes are what we should have used:
name="Jeff" echo "Hello $name" Hello Jeff
name="Jeff Brown" echo "$name" Jeff Brown echo $name Jeff Brown echo '$name' $name
Note that we were able to print a variable containing spaces without quotes.
Printing variables WITHOUT double quotes is NOT Best Practice and is in fact DANGEROUS.
Bash splits the variable contents on spaces, tabs, or newlines. If the variable contains commands they are treated as such.
name=* echo $name Laid Back - ...Keep Smiling (1983) Laid Back - Bakerman (1990) Laid Back - Cosmic Vibes (2011) Laid Back - Forevergreen (2024) Laid Back - Happy Dreamer (2004) Laid Back - Hole In The Sky (1990 DSF) Laid Back - People (2005) Laid Back - Road To Fame (2023) echo "$name" *
The asterisk is a globbing character, meaning all files in the current directory. Not something you probably want strangers to see.
If you are asking for some user input in a script, ALWAYS double quote the resulting variable.
Imagine if someone entered '/*'