Understanding Double Quotes in Bash [3 Examples]

Double quotes are a versatile component in Bash that allows variable and command substitution, insertion of escape sequences, and special characters. The more you use double quotes, the more you will construct meaningful strings or dynamic content. In this article, you will learn how to use Bash double quotes to make an intuitive interaction with your scripts.

Key Takeaways

  • Learning about the double quotes in Bash.
  • Exploring practical examples of double quotes in Bash.

Context of Double Quotes in Bash

Double quote (” “) is a flexible quoting type that serves multiple purposes in Bash such as defining strings, preventing word splitting, command and variable interpolation, and aiding the escape sequences. These double quotes are almost similar to single quotes in terms of preserving special characters. However, there are some exceptions:

Exceptions >

  • $ (Dollar-sign) → For variable expansion and command substitution.
  • ! (Exclamation symbol) → For history expansion.
  • ` (Backticks) → For altering commands.
  • \ (Backslash) → For escaping specific characters.

Double Quotes in Terms of Word Splitting

In Bash scripting, it’s so frequent to use commands or string values including characters like ‘space’, ‘newline’, and ‘tab’ typically stored in the special shell variable IFS (Internal Field Separator). Bash breaks the input commands including these characters into words. Thus word splitting occurs. In such a situation, you can enclose these characters within double quotes and prevent the word-splitting issue by making Bash interpret them as single-string values.

3 Examples of Double Quotes in Bash

In the following section you will find a glimpse of three examples of double quotes in Bash:

Example 1: History Expansion Using Double Quotes in Bash

You can use the history expansion operator (!) within double quotes to re-execute any command from your history sessions. Follow the steps below for history expansion within double quotes:

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

➋ Run the following echo commands for history expansion within double quotes:

echo “History expansion within double quotes: !-1”
EXPLANATION
  • echo: Displays text to the terminal.
  • !-1: Here, ‘i’ initiates the history expansion. And ‘-1’ is the history reference that tells Bash to execute the command that was executed exactly before the current command in your command history.
echo “History expansion within double quotes: !-3”
EXPLANATION
  • !-3: Here, ‘i’ initiates the history expansion. And ‘-3’ is the history reference that tells Bash to execute the command that was executed three commands before the current command in your command history.

History expansion within double quotes

From the image, you can see that whenever I have used the history expansion operator with a history reference number enclosed in double quotes, Bash recalls and executes the specific command according to the given reference from the command history.

Example 2: Passing Arguments Using Double Quotes in Bash

In Bash, you can easily pass arguments or parameters to the functions or shell scripts by enclosing them within double quotes. Following is an example with several steps to visualize the fact:

Steps to Follow >

➊ Open a script in the Nano text editor by running the following command:

nano function.sh
EXPLANATION
  • nano: A text editor.
  • function.sh: This is a script. Here, I have named the script ‘function.sh’. You can name any of your choices.

Open the file in Nano editor

➋ Now, write the following script inside the editor:

Script (function.sh) >

#!/bin/bash

function argcount() {
 echo $#
for p in "$@"; do
 echo $p
done
}

argcount "Now, I am in $(pwd)."
EXPLANATION

Here, in #!/bin/bash, ‘#!’ is called Shebang’ or ‘Hashbang. The portion function argcount() {…} demonstrates a function ‘argcount’ that counts and prints the user input parameters one after one. In the line echo $#, the ‘$#’ counts the argument and the echo command displays the number of the argument.

Next, ‘for p in “$@”; do … done’ depicts a loop that iterates through all the arguments passed to the function where ‘“$@”’ indicates all arguments to be treated as separate words. Finally, the line argcount “Now, I am in $(pwd).” calls the function ‘argcount’ with an argument “Now, I am in $(pwd).” that is a string encased in double-quotes.

➌ 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 function.sh
EXPLANATION
  • chmod: Changes the permission of the files and directories.
  • u+x: Adds the executable permission for the user.
  • function.sh: The file which you want to make executable.

Adding executable permission to the script

➎ Finally, run the script by the following command:

./function.sh

Parameters within double quotes passed to the function

From the image, you can see that the script calls the particular function ‘argcount’ and displays the argument count which is 1 in this case. Also, from the line ‘Now, I am in /home/nadiba’ you can understand that the script prints the entire string including the command substitution of $pwd within double quotes.

Example 3: Dealing With Nested Double Quotes

To employ the nested double quotes, you can use a backslash before the inner double quotes like ‘\”’ to indicate the double quotes as literal characters within the string.

You can follow the Steps of Example 2, to save & make the script executable.

Script (doublenest.sh) >

#!/bin/bash

#Diplaying output with double quoted word
echo "This is \"LinuxSimply\"."
EXPLANATION

In the line echo “This is \”LinuxSimply\”.”, the echo command prints the line with a nested double-quoted word “Linuxsimply”.

Now, run the script by the following command:

./doublenest.sh

Output of nested double quotes

The above image depicts that after running the script, the nested double quote has been appended to the word LinuxSimply within the string.

Conclusion

Hope you have understood well how to work with double quotes in Bash by reading the whole article. So, to conclude, it’s very important to deal with double quotes in proper ways, for proper conditions like variable & command interpretation, passing function parameters, etc.

People Also Ask

Can I prevent variable expansion within a double-quoted string in Bash?
Yes, you can prevent variable expansion within a double-quoted string in Bash by escaping the dollar symbol with a backslash (\$).

How to employ double quotes for creating multiline strings in Bash?
To employ double quotes for creating multiline strings, append newline characters (\n) within the strings in Bash.

Does Bash support a literal double quote within a double-quoted string?
Yes, Bash supports a literal double quote within a double-quoted string.

Does Bash allow double quotes within a regular expression pattern?
Yes, Bash allows the use of double quotes within e regular expression when the pattern is enclosed with a single quote.

Related Articles


<< Go Back to Bash Quotes | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
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