18

I've seen history | grep blah and history |grep blah; and history|grep blah also works, though no one ever seems to use it.

Is there any significance in the spaces (e.g. piping to/from different commands requires different use of spaces), or is it always arbitrary?

2
  • 13
    Readability, mostly.
    – jasonwryan
    Mar 1 '15 at 20:00
  • 1
    | pipe or ; semicolon are used by most shell (bash, ksh, tcsh) as separator of commands. when not enclosed in quote white space before and after are irrevelent.
    – Archemar
    Mar 1 '15 at 20:10
23

bash defines several metacharacters. From man bash:

metacharacter
A character that, when unquoted, separates words. One of the following:
| & ; ( ) < > space tab

Because metacharacters separate words, it does not matter whether they are surrounded by spaces. The pipe symbol, |, is a metacharacter and hence, as you noticed, it does not need spaces around it.

Note that [, ], {, }, and = are not metacharacters. Their meaning, by contrast, depends strongly on whether they are surrounded by blanks.

Examples of when spaces are and are not needed

As you noticed, it does not matter whether | is surrounded by spaces. Let us consider some examples that commonly confuse bash users. Consider:

$ (date)
Sun Mar  1 12:47:07 PST 2015

The parens above force the date command to be run in a subshell. Because ( and ) are metacharacters, no spaces are needed. By contrast:

$ {date}
bash: {date}: command not found

Since { and } are not metacharacters, the shell treats {date} as one word. Instead of looking for the date command, it looks for a command named {date}. Because it doesn't find one, an error results.

Another common problem is the test command. The following works successfully:

$ [ abc ] && echo Yes
Yes

Remove the spaces and an error occurs:

$ [abc] && echo Yes
bash: [abc]: command not found

Because [ and ] are not metacharacters, the shell treats [.bashrc] as a single word and the result, just like in the date example, is an error.

Assignment statements are also sensitive to spaces. The following assignment is successful:

$ v=date
$ echo $v
date

Add a space and the assignment fails:

$ v= date
Sun Mar  1 12:55:05 PST 2015

In the above, the shell temporarily sets v to empty and then executes the date command.

Add a space before = also causes a failure but for a different reason:

$ v =date
bash: v: command not found

Here, the shell attempts to execute the command v with the argument =date. The error is because it found no command named v.

1
  • 1
    I recently fell victim to the assignment statement. I had spaces before and after the =. Took a while to debug.
    – topher
    Mar 2 '15 at 8:00
4

There is one case where it can be useful to not use spaces. If you're not using a US-American layout, you may be forced to use some combinations like AltShiftL to input a pipe. While this is not a problem per se, one consequence is that sometimes you also input non printing characters before or after that character. For example, on a french Macbook Pro keyboard, I have to use AltShiftL to input |. When you type quickly, you can accidentally type this: AltShift(L,Space)

$ sudo dmesg | tail
zsh: command not found:  tail

echo "sudo dmesg | tail" | od -a
0000000    s   u   d   o  sp   d   m   e   s   g  sp   |   �   �   t   a
0000020    i   l  nl

If you're not aware that AltShiftSpace inputs a different space (the non-breaking-space (U+00A0)), the following error can be hard to understand: zsh: command not found:  tail

1
  • Same thing happened to me with a Macbook keyboard with Spanish layout.
    – cygnusv
    May 12 '20 at 17:26
3

Pipes let you use the output of a program as the input of another one ...

As far as spaces , its just matter of readability / personnel preference like @jasonwryan mentioned.

One space-bar before and after "|" is the norm ....

You can also use it with column -t , to not just make your one liner neat , but also your output.

lnydex99uhc:depot_r user$ lsof | grep my | column -t
Microsoft  290  user  txt  REG  1,4  9515016  170972    /Library/Fonts/PCmyoungjo.ttf
bash       359  user  cwd  DIR  1,4  714      12246074  /Users/zatef/hw2/base/active/myapp
1

The only significance of spaces in this case is aesthetics.
Or in other words useful to make the commands more readable to a human.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.