2009-06-24

Bash Tutorial: Part 3

We're back at it with more about using Bash. This time, rather than focus on new commands, we'll look at command operators and redirection. Command operators allow you to run multiple Bash commands in one statement. Redirection is not just a useful tool when performing magic, it's also a great way to change where the input and/or output of a command come from and/or go.

Command Operators
  • ;
    To execute multiple commands one after another, we use the ; operator. In other words, the first command specified will run until completion before the second command starts. For example, if you want to update your index for the locate command and then search for the xorg.conf, we would do the following:

    # updatedb ; locate xorg.conf

    The above is essentially the same as running this:

    # updatedb
    # locate xorg.conf

  • &
    Executing commands in rapid succession all on one line is cool, but it can be more useful to run multiple commands all at the same time. For example, if you wanted to write a Bash script to launch firefox, pidgin, and songbird all at once, the important piece would be the following command:

    $ firefox & pidgin & songbird

    Another use for the & operator is if you want to run a command in the background. In other words, if you would like to run a command, but instead of waiting for the command to finish to regain control of the terminal, the control will immediately be returned and your computer will continue executing the command in the background. To do this, you simply end your command with &. If you wanted to update your index for the locate command, but don't want to wait for this to finish, you could do the following:

    # updatedb &

Redirection
  • >
    Sometimes, the output of a command isn't immediately important, but you would like to save a record of it for later. To do that, we can use the > operator to create a new file, or overwrite an existing file, with the output of a command. For example, if you're trying to troubleshoot a networking problem, you might want someone to look at the output of the ifconfig command on your system. To save this output, you can do the following:

    # ifconfig > output.txt

  • >>
    Sometimes you want the output of a command to write to a file, but you don't want to lose the existing contents of that file. In this case, we can use the >> operator to append the output to the end of the specified file. For example, if you want to add the output of the lsmod command to the output file we just created, we could do the following:

    # lsmod >> output.txt

    If you have read some of my other posts, you have seen me use the two above redirection operators in conjunction with the echo command to create or add to files that control the behavior of some parts of the computer.

  • <
    There are also cases where you might want to specify the input of a command or program from a file. One example I can think of is if you have a text file that you would like to sort, you could use this as the input for the sort command like this:

    $ sort < unsorted.txt

    Of course, the above will simply output the text from unsorted.txt to the screen in sorted order. If we wanted to save the sorted version of the text file, we could combine input and output redirection to accomplish this:

    $ sort < unsorted.txt > sorted.txt

  • <<
    The << operator acts basically as a simple line based text editor. If you remember, we used it in the Installing Songbird post as an option for creating the Bash script to launch Songbird. Here's an example of the << operator with the sort command:

    $ sort << EOF
    > 3
    > 1
    > 2
    > EOF

    For another example, I've included the section where we used this in the Songbird post. In this example we use both input and output redirection to create a Bash script without dealing with a real text editor:

    # cat > /usr/share/Songbird/launcher.sh << EOF
    > #!/bin/sh
    > cd /usr/share/Songbird
    > ./songbird
    > EOF

  • |
    Pipes. Saving the best for last here, pipes allow you to redirect the standard output of one command to the standard input of another command. That may sound a little confusing as you read it, but let me give an example, that may clear things up:

    $ ls -al | less

    Using what we have learned about Bash command operators, the above command is basically the same thing as:

    $ ls -al > temp.txt ; less temp.txt ; rm temp.txt

    As you can see, piping commands is powerful and can save a bit of typing. My favorite use of pipes is with the grep command:

    $ ps -ef | grep firefox

    I'll cover both the ps and grep commands in future posts, but the above will give you information about any processes running on your system that have firefox as part of the process name. The above would be the same as the following:

    $ ps -ef > temp.txt ; grep firefox temp.txt ; rm temp.txt

That does it for Part 3 of the introduction to Bash.

See you next time.

1 comment: