Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)

About Spaces in the Command Line

As we have seen, the shell uses spaces to separate the parts of the command line. Having two or more spaces separate a command from its options or its arguments doesn't change anything. When your shell acts on your command line, it breaks it into pieces by looking at where the spaces are. The following command lines both do the same thing:

ls -l ls -l

But this one is very different:

ls - l

In the first two cases the shell sees two items: ls and -l . In the third case the shell sees three items: ls , - , and l .

But there are times when you have to include a space inside an argumentsuch as in a filename that itself contains spaces. Consider what would happen if you tried the command line

ls l /Desktop DB

If you don't do something special to handle spaces in command-line arguments, you will have problems. The shell treats the spaces as separators, and you will get unexpected and probably undesired results ( Figure 2.31 ).

Figure 2.31. When you use unprotected spaces in a command-line argument, the shell treats each word as a separate argument.

user-vc8f9gd:~ vanilla$ ls l /Desktop DB ls: /Desktop: No such file or directory ls: DB: No such file or directory user-vc8f9gd:~ vanilla$

Here are two ways to handle spaces safely in command-line arguments.

To protect spaces using quotes:

1.

ls -l "/Desktop DB"

When you enclose the argument in quotes, the shell treats everything within the quotes as a single entity.

2.

You may also use single quotes:

ls -l '/Desktop DB'

Tip

To protect spaces using the backslash:

Tip

Категории