Understanding Single Quotes in Bash [2 Examples With Cases]

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
EXPLANATION
  • cd: It helps to navigate to a directory.
  • Desktop: The directory I navigated.

Navigating to Desktop

➋ Create a file by running the touch command and check if the current directory includes any filename with spaces.

touch 'my file.txt'
EXPLANATION
  • 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
EXPLANATION
  • ls: Lists the contents of the current directory.

Creating file and listing content

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'
EXPLANATION
  • 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.

Renaming file using 'mv command'

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
EXPLANATION
  • 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.

Opening file in Nano editor

➋ 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"
EXPLANATION

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
EXPLANATION
  • 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.

Adding executable permission to the bash script

➎ Finally, run the script by the following command:

./special.sh

Output of special character interpretation in Bash

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 ‘\”’.

You can follow the Steps of Example 1 (Case B), to save & make the script executable.

Script (nest.sh) >

#!/bin/bash

#Diplaying output with single quoted word
echo 'This is '\''LinuxSimply'\''.'
EXPLANATION

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

Output of nested single quotes in Bash

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

Does Bash require single quotes to define function names?
No, Bash doesn’t require single quotes to define function names. You can directly define function names without using any quotes.

Does Bash support escape sequences within single quotes?
Yes, Bash supports some escape sequences like \\, \’, etc. within single quotes.

Can I use single quotes to prevent unwanted command execution in Bash?
Yes, you can use single quotes to prevent unwanted command execution in Bash.

How to use single quotes to create a HereDocument in Bash?
By enclosing the delimiter in a single quote like ‘<<‘EOF’’, you can easily create a HereDocument in Bash.

Does Bash allow array declaration using single quotes?
No, Bash doesn’t allow array declaration using single quotes. Rather you can use double quotes to do so.

Related Articles


<< Go Back to Bash Quotes | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Nadiba Rahman

Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio

Leave a Comment