FUNDAMENTALS A Complete Guide for Beginners
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
- cd: It helps to navigate to a directory.
- Desktop: The directory I navigated.
➋ Run the following echo commands for history expansion within double quotes:
echo “History expansion within double quotes: !-1”
- 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”
- !-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.
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
- 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.
➋ 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)."
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
- 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.
➎ Finally, run the script by the following command:
./function.sh
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.
Script (doublenest.sh) >
#!/bin/bash
#Diplaying output with double quoted word
echo "This is \"LinuxSimply\"."
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
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
Related Articles
- What is Quoting in Bash? [5 Hacks You Didn’t Know]
- Understanding Single Quotes in Bash [2 Examples With Cases]
- Bash Single Vs Double Quotes [3 Cases to Differentiate]
- Escape Quotes in Bash
<< Go Back to Bash Quotes | Bash Scripting Tutorial