If you utilize UNIX, you recognize however helpful the command may be for operating with files, putting in computer code, and launching programs. however it may be even additional economical if you run multiple commands right away.
Combining 2 or additional commands on the command line is additionally called “command chaining”. We’ll show you other ways you’ll mix commands on the command line
Method 1: The Semicolon (;) Operator
The semicolon (;) operator permits you to execute multiple commands in succession, not with standing whether or not every previous command succeeds. for instance, open a Terminal window (Ctrl+Alt+T in Ubuntu and UNIX operating system Mint). Then, sort the subsequent 3 commands on one line, separated by semicolons, and press Enter. this can offer you an inventory of this directory ( ls ), conclude that directory you’re presently in ( pwd ), and show your login name ( whoami ) all quickly.
ls ; pwd ; whoami
You don’t have to be compelled to place spaces between the semicolons and also the commands, either. you’ll be able to enter the 3 commands as ls;pwd;whoami . However, areas build the combined command additional legible, that is particularly helpful if you’re golf shot a combined command into a shell script.
Method 2: The Logical AND Operator (&&)
If you would like the second command to solely run if the primary command is palmy, separate the commands with the logical AND operator, that is 2 ampersands ( && ). for instance, we would like to create a directory referred to as MyFolder and so modification thereto directory–provided it had been with success created. So, we tend to sort the subsequent on the instruction and press Enter.
mkdir MyFolder && cd MyFolder
The folder was with success created, that the cd command was executed and that we area unit currently within the new folder.
We suggest using the logical AND operator instead of the punctuation operator most of the time (;). This ensures that you simply don’t do something fateful. for instance, if you run a command to vary to a directory and so force take away everything therein directory recursively ( cd /some_directory ; rm -Rf * ), you’ll find yourself destruction your system if the directory modification didn’t happen. Not that we tend to suggest you run a command to categorically take away all files in an exceedingly directory at once.
Method 3: The Logical OR Operator (||)
Sometimes you would possibly need to execute a second command provided that the primary command doesn’t succeed. To do this, we tend to use the logical OR operator, or 2 vertical bars ( || ). for instance, we would like to examine to envision if the MyFolder directory exists ( [ -d ~/MyFolder ] ) and make it if it doesn’t ( mkdir ~/MyFolder ). So, we tend to kind the subsequent command at the prompt and press Enter.
[ -d ~/MyFolder ] || mkdir ~/MyFolder
All of these methods of combining commands can also be used in shell scripts on both Linux and Windows 10.
Don’t Miss: How to use Screen command in Linux