Check out a free preview of the full Introduction to Bash, VIM & Regex course

The "Quotes" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

To escape special characters from an argument, James introduces the use of quotes.


Transcript from the "Quotes" Lesson

>> James: One thing that we've been using a little bit already is quotes, and I think it's important to talk about how quotes work exactly. So if you do some things like, say you want to have some text that has things like less thans and greater thans in it, if you just try to run the command like that, then bash is gonna interpret the less thans and greater thans probably in a way that you don't want.

And to try to create a file and write to it and try to read from a file that doesn't exist probably. So it's good to put when you're using things like literal dollar signs, and literal less thans and greater thans to put them in single quotes.
>> James: Or double quotes in some cases.

And this means literally that.
>> James: So double quotes, how they work is a little bit different because you can put things like backticks and dollar signs that will expand out to be environment variables inside of double quotes, but not single quotes. So if I say, if I say this message, hello $USER, that's not gonna work with single quotes, but it will work with double quotes.

It will expand out USER. So it's the main difference there. You can also put both forms of doing backticks like $(whoami) is another way of saying $USER or include backticks, same thing.
>> James: So you've just gotta be careful sometimes if you want literal characters that bash is gonna interpret like pipes and whatever.

Put quotes around them.
>> James: Another important thing is if sometimes you have to, you want things like spaces in the value of an argument. A good example of this is if you wanna specify a commit message with git on the command line. You do a git commit and then you can do a message with -m.

This is a great opportunity to talk about some of the ways in which switches work because there's a lot of subtle conventions here. So -am is often, but not always, the same as doing -a -m. So it's like a little bit of a shorthand that some options parsers will expose.

In certain commands like git interpret commands in this way. But it's not universally true. It's just something that's often true. You kinda have to know a little bit more about the command. But, in this case, that's what git -an means. And that only works when the commands that preface the other one are boolean commands.

It's a little technical and complicated but, anyways, that's how that works. So what I was saying is if you want to have things like spaces there is a few ways of doing that. Say I want commit message to say hello world right? If I just typed this, it wouldn't actually do what I want it to do because the m command would read this argument, hello and then, git commit command would be past world, which is not what I want.

So there're two ways to do this. You can either put a backspace in front of the space, which will work to be the message hello world. You have to do that with all of the white space if you do it this way. Or you can use- you can quote it and then that whole argument will be one chunk that the am will receive.

If that all makes sense, but you kinda run into this sort of thing fairly often so that's how that all works.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now