1

I guess everything is in the title. I am currently learning about bash scripts and I read that we can either use the "|" symbol, or the ";" symbol to execute two commands on one command line.

This question is just by curiosity, does anybody know what difference does it make?

AlessioX
  • 3,167
  • 6
  • 24
  • 40
L911
  • 89
  • 5
  • 1
    given `a | b`, the `|` (pipe) takes the output of `a` and feeds it to `b` as input. given `a; b`, you're just executing two programs sequentially. `a` runs, then `b` runs, but there's no chaining of input/output. – Marc B Apr 19 '16 at 15:18

3 Answers3

2

Semicolon lets the output of the program echo to the tty.

Pipe hands that output to the program to the right of the pipe.

E.g.

# execute foo, then execute bar,
# letting each program output to the terminal
foo ; bar

# execute foo and bar at the same time,
# hooking up the STDOUT from foo into the STDIN to bar,
# letting bar output to the terminal
foo | bar
amphetamachine
  • 27,620
  • 12
  • 60
  • 72
  • Thanks. Now I clearly understand the interest of using the pipe. However, is the semi-colon only used for presentation sake? – L911 Apr 20 '16 at 06:38
  • @L911 If you have to ask if the difference is only cosmetic, you don't understand the difference. The only advice I can give is try using the command line, then you feel comfortable, try writing your own scripts. – amphetamachine Apr 25 '16 at 22:37
  • Thanks to the other answers, some researches and test scripts I know clearly understand how it works! – L911 Apr 30 '16 at 06:07
2

These two symboles have different usage.

semicolon symbol

; is used to do some commands after each other without enter.

echo 1; echo 2; echo 3
# is equal to:
echo 1
echo 2
echo 3

pipe symbol

pipe is a interprocess communication way.
process1 can send something to process2
here, command1 and command2 are process.

command1 | command2

the output of command1 is given to command2 as an input

enter image description here

Fattaneh Talebi
  • 727
  • 1
  • 16
  • 42
1

The ; separator is sequential, as in C:

ls ; sort

In this example, first ls is run, and bash will wait() for its completion; then sort is run.

The | separator means two things: the commands around it are run in parallel, and the output of the left command is redirected to the input of the right command through a pipe :

ls | sort

There are other separators in bash: & (parallel), || and && (sequential).

Edouard Thiel
  • 5,878
  • 25
  • 33
  • Thank you, I just did not know that | was called a pipe, making my researches a bit difficult. :) – L911 Apr 20 '16 at 06:34