Bash Shell Scripting: When to use quotes

In my experience there are different situations to use each type of quote. I’ve provided this in straight text, and in a version you can copy and paste into a shell script (at the bottom).

On top of the single ‘ ‘ and double ” ” quotes, don’t forget to use back-ticks too ` `.

 

Here’s my short answer:

 

If you were to create a variable in Bash, you could do it like this:
variable=”here is some text”

Now issue that variable to the command line:

 

echo $variable
here is some text <-- there's the output     But if you use single quotes, you'll get $variable printed because single quotes prevent Bash from expanding the variable. That's and echo statement for $variable... See below: $variable     But say you wanted to use that variable in your print statement, then what? Here's where your back-ticks come into play: Single Quotes: here is my variable output --> `echo $variable` <-- there it is Double Quotes: here is my variable output --> here is some text <-- there it is     Those back-ticks are escaping the double quotes so that Bash can interpret the variable properly. Technically the $ escapes too, but this is just an example on different types of quotes we're not really talking about escape characters.     You could insert other commands into your strings too while using double quotes, like this: Single Quotes: I have `ls "$HOME" | cut -d/ -f4 | wc -l` files in my home directory! Double Quotes: I have 27 files in my home directory!     If you were to use single quotes, the shell wouldnt be able to expand the variable even with the back-ticks in place. here is my variable output: `echo $variable`       Here is this whole blog written in Bash so you can see the quotes:

echo "Im my experience there are different situations to use each type."
echo "And on top of the single ' ' and double \" \" quotes, don't"
echo "forget to use back-ticks too \` \`."  
echo
echo
echo "Here's my short answer:"
echo
echo
echo "If you were to create a variable in Bash, you could do it like this:"
echo "variable=\"here is some text\""
variable="here is some text"
echo
echo "Now issue that variable to the command line:"
echo
echo
echo "echo \$variable"
echo "$variable   <-- there's the output"
echo
echo
echo "But if you use single quotes, you'll get \$variable printed"
echo "because single quotes prevent Bash from expanding the variable."
echo "see below? That's and echo statement for \$variable"
echo
echo '$variable'
echo
echo
echo
echo "But say you wanted to use that variable in your print statement, then"
echo "what? Here's where your back-ticks come into play:"
echo
echo 'Single Quotes:  here is my variable output -->  `echo $variable`  <-- there it is'
echo "Double Quotes:  here is my variable output -->  `echo $variable`  <-- there it is"
echo
echo
echo
echo "Those back-ticks are escaping the double quotes so that Bash can"
echo "interpret the variable properly. Technically the \$ escapes too,"
echo "but this is just an example on different types of quotes"
echo
echo
echo
echo "You could insert other commands into your strings too while using "
echo "double quotes, like this:"
echo
echo 'Single Quotes:  I have `ls "$HOME" | cut -d/ -f4 | wc -l` files in my home directory!'
echo "Double Quotes:  I have `ls "$HOME" | cut -d/ -f4 | wc -l` files in my home directory!"
echo
echo
echo
echo "If you were to use single quotes, the shell wouldnt be able to"
echo "expand the variable even with the back-ticks in place."
echo
echo 'here is my variable output: `echo $variable`'
echo

 

 

And here’s a screenshot of what this looks like when run in on the BASH Command Line:

 

 

Hope you’ve enjoyed this! 🙂

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: -1 (from 1 vote)
Tagged , , , , , . Bookmark the permalink.

Comments are closed.