FUNDAMENTALS A Complete Guide for Beginners
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!
Key Takeaways
- 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:
cd Desktop
- cd: It helps to navigate to a directory.
- Desktop: The directory I navigated.
➋ Create a file by running the touch command and check if the current directory includes any filename with spaces.
touch 'my file.txt'
- touch: Creates files.
- ‘my file.txt’: The file you want to create. Here, I have named the file within a single quote as there is space between the filename.
ls
- ls: Lists the contents of the current directory.
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'
- mv: Changes the location of the file.
- ‘my file.txt’: The file you want to rename.
- ‘new file.txt’: The new name you want to give to your existing file.
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:
nano special.sh
- nano: A text editor.
- special.sh: This is a script. Here, I have named the script ‘special.sh’. You can name any of your choices.
➋ Now, write the following script inside the editor:
Script (speical.sh) >
#!/bin/bash
#Defining variable
myfile='Specialfile!.txt'
#Displaying the output
echo "This is $myfile"
Here, in #!/bin/bash, ‘#!’ is called ‘Shebang’ or ‘Hashbang’. In the line myfile=’Specialfile!.txt’, I have declared a variable ‘myfile’ by a filename including a special character (!). Next, in the line echo “This is $myfile”, the echo command displays the output.
➌ Then, press CTRL+S to save the file & press CTRL+X to exit.
➍ After that, use the command below to make the script executable:
chmod u+x special.sh
- chmod: Changes the permission of the files and directories.
- u+x: Adds the executable permission for the user.
- special.sh: The file which you want to make executable.
➎ Finally, run the script by the following command:
./special.sh
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) >
#!/bin/bash
#Diplaying output with single quoted word
echo 'This is '\''LinuxSimply'\''.'
In the line echo ‘This is ‘\”LinuxSimply’\”.’, the echo command prints the line with a nested single quoted word ‘Linuxsimply’.
Now, run the script by the following command:
./nest.sh
The above image depicts that after running the script the nested single quote has been appended to the word LinuxSimply within the string.
Conclusion
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
Related Articles
- 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
<< Go Back to Bash Quotes | Bash Scripting Tutorial