March 15, 2016

25+ Awesome Linux/Unix command chaining examples

Command chaining is a concept to execute two or more commands in one execution to increase.

productivity

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

& --Sends process background (so we can run multiple process parallel); --Run multiple commands in one run, sequentially.
	 --To type larger command in multiple lines
	&& --Logical AND operator
	|| --Logical OR operator
	! -NOT operator ( Thanks for 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
  for giving good examples)
	| -- PIPE operator
	{} --Command combination operator.
	() --Precedence operator

& – 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 $

ping -c1 google.com & scp 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 :/opt/* /opt &

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.

ls;pwd;whoami

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.

Read Full Post:   http://www.linuxnix.com/23-awesome-less-known-linuxunix-command-chaining-examples/

 

Click Here!