Bash Shell offers three types of quote characters: single quote('), double quote(") and backslash(\). In this tutorial, we will take a detailed look at single quotes.
Inside single quotes, all special characters such as $, \, <, >, (, ), {, }, >>, ", `, &, |, ;, * are ignored by the shell. Let's take a look at an example to understand it in a better way:
echo '<>(){}>>`&*|\'
Output
<>(){}>>`&*|\
Even the value of a variable is not printed when you access a variable within single quotes.
single=123 echo '$single'
Output
$single
To work around this issue, you can use double quotes.
single=123 echo "$single"
Output
123
You can also use single quotes when you are passing command-line arguments to a command to consider it as a single unit.
$grep Mohit Natani students.txt
Output
grep: Natani: No such file or directory students.txt:S001 | Mohit Natani | Computer Science | 98%
When you run the above command, it will search for Mohit in two files: Natani and students.txt. Actually, we want to search Mohit Natani in students.txt file. To do this, simply enclose Mohit Natani inside single quotes.
$grep 'Mohit Natani' students.txt
Output
students.txt:S001 | Mohit Natani | Computer Science | 98%
All white spaces enclosed between single quotes are preserved by the shell.
echo first second third echo 'first second third'
Output
first second third first second third