Single quotes are a crucial feature of Bash for encasing and manipulating strings by preserving their literal values. These quotes encapsulate text in such a way that prevents command substitution, variable interpretation, and any unintended modifications. In this article, I’ll demonstrate how you can work with Bash single quotes. Let’s explore!
- Learning about the single quotes in Bash.
- Exploring practical examples of single quotes in Bash.
What Are Bash Single Quotes?
Single quotes (‘ ‘) are a type of quote in Bash that you can use to define a string literal where all characters are symbolized as literal characters. Typically, you cannot expand or substitute any variable, command, or special characters by using these single quotes.
2 Examples of Single Quotes in Bash
The following section dictates a simple exploration of two examples of single quotes in Bash:
Example 1: Handling Filenames Using Single Quotes in Bash
When you are working with a file where the filename contains spaces, Bash defines those spaces as separators between the specified arguments. So. it’s a must to use quotes to make Bash interpret the arguments as a single file.
However, it’s worth marking that you can use only single quotes to interpret the filenames with special characters or spaces. Here, you will find two cases that may help you while dealing with filenames in Bash:
Case A: Creating & Renaming Files in Bash
If your filename contains spaces and you want to rename the file, follow the steps below:
Steps to Follow >
➊ Open your Ubuntu Terminal and navigate to Desktop by running:
➋ Create a file by running the touch command and check if the current directory includes any filename with spaces.
touch 'my file.txt'
From the image, you can see that I have used a single quote to name the file as there is space between the filename ‘my file.txt’. Otherwise, the space would separate the two words and turn them into two files ‘my’ and ‘file.txt’.
➌ Now, run the mv command to rename the file:
mv 'my file.txt' 'new file.txt'
From the image, you can observe that I have renamed the file as ‘new file.txt’ which includes a space between and so I have enclosed the whole filename with a single quote. Finally, I have checked if the file has successfully been renamed or not by using the ls command.
Case B: Interpreting Special Characters Using Single Quotes in Bash
You can use single quotes to interpret the special characters that are inside the filename. To do so follow the steps below:
Steps to Follow >
➊ Open a script in the Nano text editor by running the following command:
➋ Now, write the following script inside the editor:
Script (speical.sh) >
#Displaying the output
echo "This is $myfile"
➍ After that, use the command below to make the script executable:
chmod u+x special.sh
➎ Finally, run the script by the following command:
From the image, you can see the special character inside the filename as a literal value.
Example 2: Dealing With Nested Single Quotes
If you want to use a single quote within single quotes, you need to exit temporarily the single-quoted string, add a single quote, and then re-enter the string by using the syntax ‘‘\”’.
Script (nest.sh) >
#Diplaying output with single quoted word
echo 'This is '\''LinuxSimply'\''.'
Now, run the script by the following command:
The above image depicts that after running the script the nested single quote has been appended to the word LinuxSimply within the string.
So far you have understood that single quotes are such a powerful element in Bash that helps you work with unprocessed data that are enclosed by them. You can easily predict and control certain characters, prevent unintended issues, and preserve the raw content of a string by using these single quotes in Bash.
People Also Ask
- What is Quoting in Bash? [5 Hacks You Didn’t Know]
- Understanding Double Quotes in Bash [3 Examples]
- Bash Single Vs Double Quotes [3 Cases to Differentiate]
- Escape Quotes in Bash