Skip to Content

Expert Strategies for Command Concatenation in Linux

Overview

In this tutorial, we’ll see the different ways in which we can combine and execute multiple Linux commands efficiently. We’ll be using Bash for our examples, so there could be slight differences with other shells.

Why Combine Multiple Commands?

Executing commands one after the other in a command line is a regular activity for a Linux administrator. But, by doing so, we may face the following issues:

  • The first command can take a long time to complete — one has to wait until then to run the second command.
  • We may miss a particular command when multiple commands are run one-by-one.
  • We may misspell a particular command, which may lead to unintended consequences.
  • It’s time-consuming and annoying to run multiple commands one-by-one.

To prevent such situations and obtain the expected result, we can combine and execute multiple commands in the command line.

Concatenate Commands in Linux

The “;” operator executes all commands regardless of whether the previous ones failed or not.

After logging into the server, we can execute the following commands:

Change to logs directory (using cd)
List the latest ten log files (using ls and head)
Print the disk usage of logs files (using du)
Print the disk usage of all file systems (using df)

Nevertheless, we can concatenate all of the above tasks in a single line using the “;“ operator:

$ cd logs; ls -lt | head; du -sh ; df -h

However, we need to keep in mind that if one command in the command chain fails, the remaining commands will execute anyways. And this may produce unexpected outputs.

Concatenate Commands Conditionally in Linux

The above approach is not suitable for all situations. Such as executing commands based on the success or failure of the previous one. In such cases, we can use one of the following approaches.

4.1. Concatenate Commands With “&&“
The “&&” or AND operator executes the second command only if the preceding command succeeds.

Let’s say we want to change to a new directory using the cd command and echo some message related to the new directory. To do that, one option is to concatenate the commands with “;“:

$ pwd
/home
$ cd /tmp; echo "Now we are in $PWD"

This works fine if the cd command is successful. However, if the cd command fails, for example, if we misspelled the folder name accidentally, the echo command will be executed in the /home directory:

$ pwd
/home
$ cd /invalid-dir; echo "Now we are in $PWD"
bash: cd: /invalid-dir: No such file or directory
Now we are in /home

In our example, the second command is echo. It may print misleading messages but won’t do harm to the system.

However, if the second command is a dangerous operation, for instance, rm -rf *, then the whole command becomes: “cd someDir; rm -rf *“.

If the cd command fails, the rm command will be executed in the current directory, which is /home in our example. As a result, all contents of the home directory will be removed. This would be fatal.

To avoid this, we want the second command not to start at all if the first command fails. The solutions is replacing the “;“ operator with “&&“:

$ cd /invalid-dir && echo "Now we are in $PWD"
bash: cd: /invalid-dir: No such file or directory

As we can see, this time, the echo command doesn’t start if the cd command fails.

4.2. Concatenate Commands With “||”
The “||” or OR operator executes the second command only if the precedent command returns an error.

Let’s assume we’re creating a new shell script to archive log files. Before executing a new shell script, we need to make it executable. Let’s see how we can achieve this using the “||” operator.

First, we’ll check if it is executable. If it’s not, we’ll make it executable using the chmod command:

$ [ -x "archive_logs.sh" ] || chmod +x archive_logs.sh

In this example, the chmod command executes only if the file archive_logs.sh is not executable.

Group Commands in Linux

In Bash, we can use both “{ }” and “( )” operators to group commands.

In this section, we’ll address how to use them to group commands and understand the difference between them through examples. Also, we’ll discuss the everyday use-cases of grouping commands.

 Grouping Commands Using “{ }“
The syntax to use curly braces “{ }” to group commands is:

{ command-list; }

Note that the semicolon (or newline) following the command list is required.

Let’s see an example of grouping four commands using “{ }“:

$ { echo "Hi there"; pwd; uptime; date; }
Hi there
/tmp/test/baeldung
 20:27:11 up 30 days,  5:36,  1 user,  load average: 0.26, 0.59, 0.77
Wed 19 Aug 2020 08:27:11 PM CEST

Grouping Commands Using “( )“
The syntax to use parentheses “( )” to group commands is quite similar, except that the semicolon following the command list is optional:

( command-list )

Let’s group the same commands using “( )“:

$ ( echo "Hi there"; pwd; uptime; date )
Hi there
/tmp/test/baeldung
 20:34:05 up 30 days,  5:43,  1 user,  load average: 0.97, 0.86, 0.81
Wed 19 Aug 2020 08:34:05 PM CEST

The Difference Between “{ }” and “( )“
There is just one single difference between command grouping using “{ }” and “( )“:

  • { Commands; } : Commands execute in current shell
  • ( Commands ) : Commands will execute in a subshell

When Do We Need to Group Commands?
Primarily, we need to group commands in two scenarios:

  • Apply same redirections on a list of commands
  • Apply logical operators on a list of commands

Multiple Commands in Background
To execute a single command in the background mode, we can use the “&” operator. But to execute multiple commands in the background, we can use one of two ways:

  • Use “&” along with “&&“
  • Use “&” along with command group

 

 Conclusion

In this tutorial, we saw the different ways in which we can combine and execute multiple commands in the command line efficiently.

Operator “;“ can be used if the execution of multiple commands is unconditional. Whereas operators “&&” and “||” can be used if the execution of the command is based on the success or failure of the previous command.

Moreover, we addressed two ways to group commands and discussed the difference between them.

Also, we saw how we could execute multiple commands in the foreground and the background.

However, to regularly run multiple commands in a particular order, we may need to put all those commands in a shell script.