Command chaining is a concept to execute two or more commands in one execution to increase.
Reduce system resource usage (In some cases)
Short and sweet codes.
These are supported by almost every shell we know.
Today we will learn how to use different command chaining operators available for us in an easy way.
Command chaining operators
& – Runs a command in the background
This operator is useful to send a process/script/command to background, so that we can execute other commands in foreground to increase effective utilization of system resources and to speed up the script execution. This is also called as Child process creation or forking in other programming languages.
Example 1: Run commands in the background
$ping -c1 google.com &
Example 2: Run more commands in the background in single line $
Above commands are run in the background parallel independent of other commands. Like this, we can run many commands parallel.
; – semicolon operator
This operator Run multiple commands in one go, but in a sequential order. If we take three commands separated by semicolon, second command will run after first command completion, third command will run only after second command execution completes. One point we should know is that to run second command, it do not depend on first command exit status.
Example 3: Execute ls, pwd, whoami commands in one line sequentially one after the other.
Note: The number of commands you can run is infinity as we said earlier. By default there is no limit on how many commands you can run with ; operator. We have checked this with 500 commands executed in one line. The limit depends only on memory or ulimits settings.