Go2Linux | Linux Operating System

A site dedicated to: Linux Operating System

String and pipe command together in Linux shell

Date: 2011-01-07 00:00:00 -0400

Introduction

When you are working in the Linux command line, you often need to type several commands in a succession, you can type one, wait until it finishes and then type the next one, or you can type all of them in a line, and they will execute in order.

There are various ways to execute commands in a line, I mean one after the other, we’ll see some of them in this article, so you can pick the best for your needs depending on the situation.

String commands in Linux using semi colon

This is the simplest way, for example:

htop ; free ; df -h

What you will get is htop executed, once you leave it, free will execute and output its info to the screen, immediately you will see the output of df -h on your screen.

It does not matter if the command is successful or not. So if you made a mistake and type this:

fre ; df -h

The output will be something like this:

bash: fre: command not found
Filesystem            Size  Used Avail Use% Mounted on
udev                   10M  212K  9.8M   3% /dev
/dev/disk/by-uuid/fc8f5296-7bd8-463a-87af-87c6d2215ff9
                       41G   23G   16G  59% /
none                 1004M  102M  903M  11% /dev/shm

You get the command not found error and then df -h is executed, so if you need that the first command be successful to execute the second or third one, do not use this method.

String commands in Linux using double ampersand (&&)

This is maybe one of the most known ways to string commands together in Linux, it works more or less the same as with the semi-colon, but it will check for errors in each command on the line before going to the next one.

This is great if you are compiling software to install it. For example:

./configure && make && make install

This way if any of the commands configure or make gives any error, the next one in the line will not be executed and all the execution is stopped.

String commands in Linux using double pipe (||)

With the double pipe “||” you are telling the shell that it should only execute the next command if the first one has errors. You can use it, if you want to perform an action and there are two ways to do it, so, you can join both of them with double pipes, and if the first one fails the second one will be executed.

Just as an example.

vmstat || free

So, only in the event you do not have vmstat installed (do not really know if that is possible) free will be executed, and you will have one way or the other the memory info your Linux box.

I’m sure you will find better applications :).

Pipe commands together with pipe (|)

Using a single pipe “|” to string Linux commands together will make another interesting thing, it will forward the output of one command to the other, the next command in the line, will use that output as its input.

I use this a lot when writing post for my blog, I write my post using vim and with markdown syntax, then I use the perl program to translate it to html, but also use smartypants, so I use a line like this:

/html-tools/markdown.pl mypost.txt | /html-tools/smartypants.pl | xclip -selection clipboard

That way, markdown.pl translate mypost.txt to html, then smartypants.pl adds the typographic punctuation HTML entities, and finally its output goes to my clipboard using xclip which I can just paste in my drupal blog.

Final notes

As you can see pipe commands in Linux is easy, and you have one option for each need, hope you find the one for your needs here.

If you have more examples or better ways to achieve these task, please share in the comments.

If you liked this article please share it.

powered by TinyLetter

If you want to contact me in any other way, please use the contact page.