Skip to main content
Ellane learns the command line

Escape arguments, completely

Let's use these annoying spaces to go a little deeper on the concept of arguments.

We've already met them. We have a command which is optionally followed by an argument.

cd ~/Desktop
`# └─â”Ŧ─────┘`
`#   the argument`

Arguments are sometimes called parameters. If the command is the thing that does a thing, the argument is the thing it does it to.

cd to where?

ls which folder?

So far we've seen zero- and one-argument commands. But many commands can take multiple arguments; many commands need multiple arguments.

Think about copying a file: you have to know which file to copy and then where to copy it to.

Two arguments. And we separate them by ... you guessed it ... a space.

ls will accept more than one argument. Just give it more than one folder to list.

ls ~/Desktop ~/Downloads
`# └─â”Ŧ─────┘ └─â”Ŧ───────┘`
`#   first     second`

So now it should be obvious why trying to list out a folder with a space in its name doesn't work. The CLI doesn't know that it's one folder: actually, you gave it two arguments, and neither is the name of a folder!

How to 'escape' this problem #

This is obviously a very common issue. The solution is a technique called escaping.

When we escape a character, we're telling the CLI: treat the next character as exactly what it is.

So if it's a space, just be a space. Don't be the boundary between two arguments.

The escape character in your CLI's language is \. Put a backslash before any character to 'escape it'.

`#   one argument`
`# ┌─┴───────────────────┐`
cd My\ great\ folder\ name
`#   ^      ^       ^`
`#  escape characters`

Not just spaces #

Create a folder on your Desktop called "A_folder_in_quotes" and see if you can cd to it.

You'll need to escape those double-quote characters.

cd ~/Desktop/\"A_folder_in_quotes\"
`#           ^                   ^`
`#           the escape characters`

It's ugly. You never get used to it; you just have to learn to live with it.

Remember, any character #

It's worth reinforcing that this works before any character. Not just special characters.

So in our continuing series of it-works-but-you'd-never-actually-do-it, this is perfectly valid.

cd ~/\D\e\s\k\t\o\p

You're saying 'be a D!', 'be an e!', 'be an s!' ... but that's what they were anyway. So nothing changes.

Tab completion saves the day #

You'll drive yourself mad -- and get it wrong very, very often -- if you try to manually escape your file paths. Fortunately, there's an easier way.

The CLI will auto-complete a file path for you if you press the tab key. I'll denote that as <tab> in these code blocks.

cd ~/A<tab>

There's only one folder in my home folder that starts A, and it's Applications. Hitting tab autocompletes it for me.

Note that it doesn't press return for you. It doesn't run the command: you might have more to type before you're ready for that.

Multiple results #

On this Mac, I've got five folders in my home folder that start with D: Databases, Desktop, Documents, Downloads, and dev.

So try that:

cd ~/D<tab>

Your CLI will show you the possibilities. Now you can do two things.

  1. Type some more characters to disambiguate your intent, and hit tab again.
  2. Mash the tab key and watch it cycle through all the options for you. Stop when it reaches the one you want.

Pretty cool![1]

What's this got to do with spaces? #

Try tab completion on a folder with a space in it and find out. :-)

Homework #

1. "A folder in quotes" #

Make sure you can cd in to "A folder in quotes". Note the spaces in this version.

You might have to escape something yourself to get started, but as soon as you can you should use tab completion to finish the job.

2. this\that #

Using Finder, create a folder on your Desktop called this\that.

Now, without using tab completion, try to cd to it.

3. Too much escaping #

Earlier, we saw that this worked.

cd ~/\D\e\s\k\t\o\p

But this does not work. Why?

cd \~/\D\e\s\k\t\o\p
`# ^ extra escape character here`

  1. For extra nerd points think about how easy this might make navigating a Johnny.Decimal hierarchy... ↩ī¸Ž