Apr 13 2007

LCoTD: The Shell (Part 3: IO Redirection, Combining Commands, Quoting, and More!)

Published by georgegumpert at 10:39 am under Linux Command of the Day, Linux Resources, Series ()

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

This one’s a real doozie. In today’s Linux Command of the Day, I’ll be covering Input/Output redirection, pipes, combining commands, quoting, escaping, command-line editing, command history, and filename completion. I know it sounds like a lot, but it’s all really quite simple, and not too much to take in! But that doesn’t mean it isn’t essential to know- in fact, quite the opposite! This information could save your life one day (ok, maybe I’m being a bit dramatic for 99.98% of my readers, but for that 0.02%, you’ll be glad you read this!)

Input/Output Redirection

The shell can redirect standard input, standard output, and standard error to and from files. In other words, any command that reads from standard input can have its input come from a file instead with the shell’s < operator:

$ mycommand < infile

Likewise, any command that writes to standard output can write to a file instead:

$ mycommand > outfile
$mycommand >> outfile

The first example will create/overwrite an outfile, while the second will append to the outfile.

A command that writes to standard error can have its output redirected to a file as well:

$ mycommand 2> errorfile

To redirect both standard output and standard error to files:

$ mycommand > outfile 2> errorfile
$ mycommand > outfile 2>&1

The first example outputs to two seperate files (outfile and errorfile), while the second just outputs to a single file (outfile)

Pipes

Using the shell, you can redirect the standard output of one command to be the standard input of another, using the shell’s pipe (|) operator. For example:

$ who | sort

sends the output of who into the sort program, printing an alphabetically sorted list of logged-in users.

Combining Commands

To invoke several commands in sequence on a single command line, separate them with semicolons:

$ command1 ; command 2 ; command3

To run a sequence of commands as above, but stop execution if any of them fails, separate them with && (”and”) symbols:

$ command1 && command2 && command3

To run a sequence of commands, stopping execution as soon as one succeeds, separate them with || (”or”) symbols:

$ command1 || command2 || command3

Quoting

Normally, the shell treats whitespace simply as separating the words on the command line. If you want a word to contain whitespace (e.g., a filename with a space in it), surround it with single or double quotes to make the shell treat it as a unit. Single quotes treat their contents literally, while double quotes let shell constructs be evaluated, such as variables:

$ echo 'The variable HOME has value $HOME'
The variable HOME has value $HOME
$ echo "The variable HOME has value $HOME"
The variable HOME has value /home/smith

As you can see, just switching from the single quote () to the double quote () caused the shell to output the value of the variable, not the name.

Backquotes cause their contents to be evaluated as a command: the contents are then replaced by the standard output of the command:

$ /usr/bin/whoami
george
$ echo My name is `/usr/bin/whoami`
My name is george

Escaping

If a character has special meaning to the shell but you want it used literally (e.g., * as a literal asterisk rather than a wildcard), precede the character with the backward slash “\” character. This is called escaping the special character:

$ echo a*
aardvark agnostic apple
$ echo a\*
a*
$ echo "I live in $HOME"
I live in /home/george
$ echo "I live in \$HOME"
I live in $HOME

You can also escape control characters (tabs, newlines, ^D, and so forth) to have them used literally on the command line, if you precede them with ^V. This is particularly useful for tab (^I) characters, which the shell would otherwise use for filename completion (as you will see further in this article).

$ echo "There is a tab between here^V^Iand here”

There is a tab between here and here

Command-line Editing

bash lets you edit the command line you’re working on, using keystrokes inspired by the text editors emacs and vi. To enable command-line editing with emacs keys, run this command (and place it in your ~/.bash_profile to make it permanent):

$ set -o emacs

For vi keys:

$ set -o vi

emacs keystroke vi keystroke
(First type esc)
Meaning
^P or up arrow k Previous command line

^N or down arrow

j Next command line
^F or right arrow l Forward one character

^B or left arrow

h Backward one character
^A 0 Beginning of line
^E $ End of line
^D x Delete next character
^U ^U Erase entire line

Command History

You can recall previous commands you’ve run- that is, the shell’s history- and then edit and re-execute them. Some useful history-related commands are listed below.

Command Meaning
history Print your history

history N

Print the most recent N commands in your history
history -c Clear (delete) your history

!!

Previous command
!N Command number N in your history
!-N The command you typed N commands ago
!$

The last parameter from the previous command; great for checking that files are present before removing them:

$ ls a*
$ rm !$

!* All parameters from the previous command

Filename completion

Press the TAB key while you are in the middle of typing a filename, and the shell will automatically complete (finish typing) the filename for you. If several filenames match what you’ve typed so far, the shell will beep, indicating the match is ambiguous. Immediately press TAB again and the shell will present the alternatives. Try this:

$ cd /usr/bin
$ ls un

And that’s it for today! Tomorrow I’ll start covering the commands that deal with Job control. Stay tuned for more!

Sphere: Related Content

If you found this article useful and use StumbleUpon, please give it a thumbs up so more people can read it! Thank you!

Please take the time to check out this thread and leave a comment letting me know what you would like to see from this site. It's still relatively young and trying to find its way- you can make design pitstop the resource you always wanted!

Leave a Reply