Bash Variables: To quote, or not to quote...

Jimmy McBride - Dec 18 '19 - - Dev Community

echo "$VARIABLE"

Should you ALWAYS wrap your variables in quotes??? Some would say, "Yes! Absolutely, always wrap your variables in quotes. Just to be safe!" Well, I say, "You do you, dawg".

Fortunately, I'm not just going to leave you hanging there. Let's explore the differences between wrapping our variables in quotes and see what the differences are. Then, we can determine for ourselves, when is the best time to use quotes around our variables. All the time, or some of the time? Decide for yourself!

$HOME and $PATH

echo $HOME
home/jimmy

echo "$HOME"
home/jimmy
Enter fullscreen mode Exit fullscreen mode

Well, it doesn't seem like there's much of a difference here! Let's create a variable with some spaces in it and see what happens.

NAME="Jimmy     McBride"

echo $NAME
Jimmy McBride

echo "$NAME"
Jimmy     McBride
Enter fullscreen mode Exit fullscreen mode

Now, we're getting somewhere! It seems like if we have more than 1 empty space in a variable and if those spaces are important we need wrap our variable in quotes!

Rule of thumb: If your variable contains more than 1 consecutive white space and that white space is important for any reason then you DEFINITELY want to wrap your variable in quotes.

Let's create a variable with a wildcard (*) and see what happens...

NAME="Jimmy McBride *"

echo $NAME
Jimmy McBride bin calc Desktop Documents Downloads Games indicator-bulletin Music Pictures Postman Public Templates Videos
Enter fullscreen mode Exit fullscreen mode

Uh-oh! Looks like, instead of printing out "Jimmy McBride *" It printed out "Jimmy McBride" plus all of our files in our home folder. That's most likely not what we wanted.

NAME="Jimmy McBride *"

echo "$NAME"
Jimmy McBride *
Enter fullscreen mode Exit fullscreen mode

Now that looks a whole lot better!

Rule of thumb: If you have a wildcard character (*) in your code and you want it to act as a regular character you DEFINITELY want to wrap your variable in quotes. However, if you intend for it to be and act as an actual wildcard, then you should NOT wrap your variable in quotes.

Let's play around a bit with putting variables in quotes...

NAME="Jimmy McBride"

echo $NAME $HOME
Jimmy McBride /home/jimmy

echo $NAME       $HOME
Jimmy McBride /home/jimmy

echo "$NAME       $HOME"
Jimmy McBride       /home/jimmy

echo $NAME * $HOME
Jimmy McBride bin calc Desktop Documents Downloads Games indicator-bulletin Music Pictures Postman Public Templates Videos /home/jimmy

echo "$NAME" * "$HOME"
Jimmy McBride bin calc Desktop Documents Downloads Games indicator-bulletin Music Pictures Postman Public Templates Videos /home/jimmy

echo "$NAME * $HOME"
Jimmy McBride * /home/jimmy
Enter fullscreen mode Exit fullscreen mode

Conclusion

If you variable doesn't contain consecutive white spaces that are important or if you have a wild card symbol that you don't want to behave as a wildcard, you definitely want to wrap you variable in quotes.

If you have a variable with a wildcard and you want it to behave as a wildcard, then you definitely want to wrap that puppy in quotes.

If the variable you are calling doesn't have important consecutive white spaces or contain a wildcard character then it doesn't seem to make a difference whether or not you use quotes or not. In that situation, especially when I know what the variables are, I would save my self a few keystrokes by not adding quotes. However, if you are ever uncertain when using a variable, wrapping it in quotes just to be safe might be a really good idea!

What are your thoughts? Did I miss anything? What's your opinion? Let me know in the comments! Thanks! 😄

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .