Bash Operators in Linux

Bash Operators in Linux

Although Linux Distributions provide a Graphical User Interface just like any other operating system, the ability to control the system via the command line interface (CLI) has many benefits. A way to control how tasks are executed or how input and output is redirected, can be done using operators.

1) Command “chaining”

  • Sequence: ;

Executing multiples tasks, one after another. After the first task is completed, the next task will execute. After the second is completed, the third will execute and so on:

Bash Operators in Linux

  • Parallel execution: &
Execute tasks at the same time.

Command chaining


2) Conditional execution:

  • And operator: &&
Task 2 will be executed only if Task 1 was successful

Bash Operators Conditional Execution

  • Or operator: ||
Task 2 will be executed only if Task 1 was unsuccessful

Bash Operators Conditional Execution Task


3) Pipelines: |, |&

  • Pipe operator: |
Used to send output from Task 1 (stdout) to input of Task 2 (stdin)

Bash Operators Pipelines

  • Pipe error operator: |&

Error output of Task1 (stderr) is also redirected to input of Task 2 (stdin)

Bash Operators in Linux Pipelines 

4) Input / output redirection

  • Redirecting input: < 

Used for command that read their input from the terminal. Input is read from from file, instead of terminal

Syntax: command < file

Bash Operators Input / Output Redirection

  • Redirecting input – here documents: <<

Used when the input of a command should be taken directly from the terminal, rather that from a file

Bash Operators Linux Input / Output Redirection

  • Redirecting input – here string:  <<<
Used when a single string should be redirected to the input of the command

Bash Operators Redirecting Input

  • Redirecting output – stdout: >, >>

Used for commands that write output to the terminal.

Syntax: 

command > file – write output to file. If file doesn’t exist, it is created. If it exists, it will be truncated

command >> file – write output to file. If file doesn’t exists, it is created. If it exists, output will be appended

Note: Stderr is still written to the terminal

Bash Operators Syntax

  • Redirecting output – stderr: 2>, 2>>

Sends stderr to file.

Syntax: 

command 2> file – write stderr to file. . If file doesn’t exist, it is created. If it exists, it will be truncated

command 2>> file – write stderr to file. If file doesn’t exist, it is created. If it exists, output will be appended

Note: Output (stdout) is still written to the terminal

Bash Operators in Linux Syntax

  • Redirecting output – stdout and stderr: &>, &>>

Used to redirect both stdout and stderr for the same command

Syntax: 

command &> file – send stdout and stderr to file. If file doesn’t exist, it is created. If it exists, it will be truncated

command &>> file – send stdout and stderr to file. If file doesn’t exist, it is created. If it exists, output will be appended

Bash Operators command file

Interested in finding out more about Linux? Check out our trainings

Florin Simion
Linux Expert