Command separators in Linux

March 23, 2021

Commands can also be combined in such a way that they are executed in a particular sequence.

Command1; command2

A command line can consist of multiple commands. Each command is separated by a semicolon, and the command line is terminated with a newline. The exit status is that of the last command in the chain of commands.

The first command is executed, and the second one is started as soon as the first one has finished:

$ w; date


satish@app:/home/satish$ w; date
 12:55:08 up 276 days, 22:29,  2 users,  load average: 5.72, 4.10, 3.60
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
satish   pts/0  10:26    4.00s  0.45s  0.07s sshd: satish [priv]
Fri Mar 21 12:55:08 IST 2021
$ w ; date > whoandwhen

Output from the date command will be redirected to the whoandwhen file.

In the preceding example, we can see that when we put multiple commands on the same line, but separated by the ; command, then those commands execute sequentially one by one:

$ date; who am iTue Mar 10 23:21:38 PDT 201student  pts/0        2015-03-10 23:12 (:0.0)

In the preceding example, the date command is executed first and the who am I command will be executed next. Both the commands are typed on the same lines, separated by the ; command.

Command grouping

Commands may also be grouped so that all of the output is either piped to another command or redirected to a file:

$ ( ls; pwd; date ) > outputfile

The output of each of the commands is sent to the file, outputfile. The spaces inside the parentheses are necessary:

$ ( w ; date ) > whoandwhen

The output of the w command and date will be redirected to the whoandwhen file:

$ (echo "***x.c***";cat x.c) > log.txt


This redirects the content of x.c with a heading ***x.c*** to the file out:

$ (pwd; ls; date) > log.txt


This redirects the output of commands pwd, ls, and date in the log.txt file.

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur


Submit a Comment

Your email address will not be published. Required fields are marked *

3 × two =