Ubuntu – meaning of “’” (apostrophe) in terminal commands

command linesyntax

For some weird reason, I tried "'" as word separator in identifiers, as in:

$ export foo'bar=1
> 
> ^C
$ 

The result is as shown above, id est the prompt changes to ">" and the system obviously waits for more information on my part. As you see, I had to ctrl-C to escape. The same happens using other commands such as

$ ls foo'bar
$ mkdir foo'bar

I'm curious about the sense of all this, that is what special meaning "'" has in terminal commands (so special in fact that it is taken apart even inside ids).

Extra: As an aside I would also like to know the set of special or otherwise invalid characters in file and folder names, both under Unix-like and Windows systems (to get the common subset of safe ones) (in particular, what about all non-ASCII?).

Thank you,
Denis

Best Answer

The ' character is a very powerful character whenever used in any shell command. Basically the ' (apostrophe marks) disables all kinds of transformations or modifications. It would consider whatever is enclosed with the ' marks as a single entity i.e. a single parameter. Absolutely no sort of substitution or expansion would take place.

Example:

 $ echo '$HOME'

would produce at the output the string $HOME itself and would not print the path to your home directory. Since the single quotes prevents any sort of expansion, substitution and simple considers whatever to be present as a simple parameter in itself.

If you want to use the apostrophe as it is, you have to escape it:

 $ mkdir foo\'bar

 $ cd foo\'bar

If it is not escaped, it will wait for it's pair to be closed, like it happened in your first example.

So, corrected, your command will be:

 $ export foo\'bar=1

NOTE: As Milan Todorovic noticed, this will not be valid, because you cannot use apostrophe in this case.

Related Question