{"id":3242,"date":"2025-02-19T20:32:20","date_gmt":"2025-02-19T20:32:20","guid":{"rendered":"https:\/\/foghosting.com\/top-10-useful-chaining-operators-in-linux-with-examples\/"},"modified":"2025-02-19T20:32:20","modified_gmt":"2025-02-19T20:32:20","slug":"top-10-useful-chaining-operators-in-linux-with-examples","status":"publish","type":"post","link":"https:\/\/foghosting.com\/blog\/top-10-useful-chaining-operators-in-linux-with-examples\/","title":{"rendered":"Top 10 Useful Chaining Operators in Linux with Examples"},"content":{"rendered":"<div>\n<p>An operator used in between several commands allows you to link them in a chain that executes them based on the behavior of the operator used between them. A Linux command chain is similar to writing shell scripts directly from the command line. Automating them is therefore possible. Chaining operators can also help an unattended machine function more efficiently.<\/p>\n<p>This article will shed light on commonly used command-chain <strong>linux operator<\/strong>, with brief descriptions and examples of <strong>linux &#038; operator terminal<\/strong> that will surely increase your productivity and let you write shorter, more meaningful codes while reducing some system load.<\/p>\n<h2><span id=\"Chaining_Commands_in_Linux\">Chaining Commands in Linux<\/span><\/h2>\n<p><strong>In Linux<\/strong>, we can chain commands to execute multiple commands simultaneously and directly from the terminal. This is similar to short shell scripts you can execute directly through the terminal. A Linux command chaining technique consists of merging several commands into one that will execute in sequence depending on the operators that separate them, and these operators determine how the commands will be executed by <strong>\u00a0<\/strong><strong>logical operators in shell script<\/strong>. The technique can run several commands at the same time.<\/p>\n<p>Chaining with chaining operators<\/p>\n<h3><span id=\"1_Ampersand_Operator\">1. Ampersand (&#038;) Operator:<\/span><\/h3>\n<p>\u00a0It is used to execute a command in the background without interrupting other commands. Processes\/scripts\/commands are sent into the background to execute other commands in the foreground. Script execution speeds up and the operator better utilizes system\u00a0resources in shell script. Other programming languages refer to this as creating a child process or forking. Ampersands can be used in the following ways:<\/p>\n<pre><code>ping -cl google.com & #change the command before &<\/code><\/pre>\n<pre><code>ping -c1 google.com & ping -c1 geeksforgeeks.org &<\/code><\/pre>\n<h3><span id=\"2_AND_Operator\">2. AND (&#038;&#038;) Operator:<\/span><\/h3>\n<p>\u00a0A command following this operator will only be executed if the preceding command is successful. If the first command has been executed successfully, it is useful to execute another command after it has been successfully executed.<\/p>\n<pre><code>echo \"hello there\" && echo \"this is gfg\"<\/code><\/pre>\n<pre><code>apt update && echo \"hello\"<\/code><\/pre>\n<pre><code>AND chaining operator<\/code><\/pre>\n<h3><span id=\"3_Semi-colon_Operator\">3. Semi-colon(;) Operator:<\/span><\/h3>\n<p>\u00a0In this way, multiple commands can be executed sequentially in one go. Nevertheless, it is crucial to note that the commands chained by the (;) operator are always executed sequentially. Separating two commands by the operator will always cause the second command to execute independently of the first. The second command is executed independently of the exit status of the first command, unlike the ampersand operator. The second command will always be executed even if the first command fails, i.e., the exit status is not zero.<\/p>\n<pre><code>who;pwd;ls<\/code><\/pre>\n<p>semicolon chaining operator<\/p>\n<p>Sequentially, each command will be executed. Regardless of whether the execution of the first command is successful, the command following the ( ; ) operator will still execute.<\/p>\n<h3><span id=\"4_Logical_OR_Operator\">4. Logical OR (||) Operator:<\/span><\/h3>\n<p>The command following this operator will only be executed if the command preceding it has failed. That is, it is the same as an else statement. The second command will be executed if the first command\u2019s execution status is not zero.<\/p>\n<pre><code>echo \"hello there\" || echo \"This is gfg\"<\/code><\/pre>\n<pre><code>apt update || echo \"hello\"<\/code><\/pre>\n<pre><code>or chaining operator<\/code><\/pre>\n<h3><span id=\"5_Piping_Operator\">5. Piping (|) Operator:\u00a0<\/span><\/h3>\n<p>The output of the first command is sent to the input of the second command with this operator.<\/p>\n<pre><code>ls -l | wc -l<\/code><\/pre>\n<p>The wc -l command in the above command displays the number of lines. ls -l displays the lists the files in the system.\u00a0 This command displays how many files reside in the directory. The output of ls \u2013 l is passed to the next command, which counts the lines. As a result, we can find out the number of files in a directory by using pipe.<\/p>\n<p>piping operator<\/p>\n<h3><span id=\"6_NOT_operator\">6. NOT (!) operator:<\/span><\/h3>\n<p>\u00a0When used in command\/, it negates an expression. Using this, all but one file in a directory is deleted.<\/p>\n<pre><code>touch a.txt b.txt c.txt d.txt e.txt<\/code><\/pre>\n<pre><code>rm -r !(a.txt)<\/code><\/pre>\n<p>A.txt is the only file that will be removed from the directory with this command. The image below shows how we created some files inside a directory using the touch command. Directory listing shows what files are present. The use! command deletes all files except a.txt. The operator. A.txt is the only file that has been removed, as we can see if we list the files again.<\/p>\n<p>NOT operator<\/p>\n<h3><span id=\"7_Redirection_Operators\">7. Redirection Operators(\u2018<\u2018,\u2019>\u2019,\u2019>>\u2019):<\/span><\/h3>\n<p>\u00a0A group of commands or a group of commands can be redirected to a stream or file using this operator. Standard input and output can be redirected with this operator. Redirection operators are supported by almost all commands.<\/p>\n<pre><code>cat >>file_name<\/code><\/pre>\n<pre><code>sort <file_name<\/code><\/pre>\n<p>With the first command we create a file named \u201cfile_name\u201d (with the redirection operator >> we can input information into the file) and then with the second command we sort the contents of that file. As you can see in the image below, we must first create a file with numbers before using this command. The contents are then sorted.<\/p>\n<h3><span id=\"8_AND_OR_Operators_as_an_if-else_condition\">8. AND, OR Operators as an if-else condition:\u00a0<\/span><\/h3>\n<p>If-else statements can be used with this command. These are logical ANDs and logical ORs.<\/p>\n<pre><code>[ ! -d ABC ] && mkdir ABC || cd ABC<\/code><\/pre>\n<p>First, this command will determine whether the directory \u2018ABC\u2019 exists or not. Otherwise, a new directory will be created if it doesn\u2019t already exist, otherwise, \u2018ABC\u2019 will become the current directory. If you look at the image below, we create the directory \u2018ABC\u2019, as it doesn\u2019t exist. The directory already exists the second time the command is run, so \u2018ABC\u2019 becomes the current directory.<\/p>\n<h3><span id=\"9_Concatenation_Operator\">9. Concatenation Operator():\u00a0<\/span><\/h3>\n<p>\u00a0A shell command that concatenates several commands into one. The result is easier to read. It is used to execute large commands because a large command is split across multiple lines.<\/p>\n<h3><span id=\"10_Precedence\">10. Precedence:\u00a0<\/span><\/h3>\n<p>To execute multiple commands in a specified order, this command sets the precedent value.<\/p>\n<p>The first command will be executed if it is successful, but the second command will not be executed. In the second case, however, the third command will be executed since the () operator is used to set precedence. Refer to the following image: If the directory already exists (the first command), then the current directory becomes PQR (the second command), but in the first case the third command does not get executed, but in the second case, the third command is executed.<\/p>\n<p>Also Read: Learn to use SCP Command in Linux (with Examples)<\/p>\n<h2><span id=\"Conclusion\"><strong>Conclusion<\/strong><\/span><\/h2>\n<p>Throughout this tutorial, we\u2019ve seen two common ways to chain commands by <strong>shell script operators bash<\/strong>: inline and waiting for other processes to finish. You can also chain commands through variables. The possibilities are endless in <strong>operator shell scripting operators<\/strong>. We can even Use user-defined functions, script files, conditionals, or loops to implement these techniques.<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>An operator used in between several commands allows you to link them in a chain that executes them based on the behavior of the operator used between them. A Linux command chain is similar to writing shell scripts directly from the command line. Automating them is therefore possible. Chaining operators can also help an unattended<\/p>\n","protected":false},"author":3,"featured_media":3076,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-3242","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dedicated-servers"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts\/3242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/comments?post=3242"}],"version-history":[{"count":0,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts\/3242\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/media\/3076"}],"wp:attachment":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/media?parent=3242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/categories?post=3242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/tags?post=3242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}