When Do Spaces Matter?

Command names and arguments are separated by spaces, but the number of spaces around a name and its arguments doesn't matter. These commands are equivalent:

% sort -r file
% sort -r file
%    sort -r file

In the commands shown thus far, I've usually put spaces around the following special characters:

; < > | ( ) &

Why did I insert the spaces? Simply, to make the commands easier to read. These characters don't require spaces around them, so the following commands are equivalent:

% ( date ; ls -s | tail +2 | sort -rn ) > sizes &
% (date;ls -s|tail +2|sort -rn)>sizes&

If an argument contains spaces, quote it to keep the shell from interpreting it as multiple arguments. For instance, when using mail to send someone a file, it's helpful to provide a subject using the –s option, so that the recipient can tell easily what the message is about. If the subject consists of several words, quote it:

% mail -s 'Here are the figures you requested' karen < budget

Without quotes, mail will think the subject is "Here" and the recipients are "are", "the", "figures", "you", "requested", and "karen".

You should also use quotes when a command argument contains other special characters, such as the parentheses and & in the following command:

% mail -s 'Playoff Schedule (for May. 6 & 7)' ian < playoffs

Get Using csh & tcsh now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.