Welcome!

Linux Authors: Michael Sheehan, Lavenya Dilip, Ian Thain, Bruce Armstrong, Ellen Rubin

Related Topics: Linux

Linux: Article

Wicked Cool Linux

Shell scripting basics

In a lot of ways, Linux seems pretty similar to other modern operating systems, with overlapping windows, a mouse, games, an e-mail program that talks to Outlook, an application that can read and write Microsoft Word documents, etc. What's different, though, is that Linux is built atop a very powerful underlying kernel that supports a wide variety of hardware and offers all users access to its power without any of the graphical layers that get in the way of real power users.

That's what this column is all about: cracking open the shell, learning how you can really exploit the power and capabilities of the command line, and how you can dip your toe into the water and learn about programming basics through the painless world of Linux shell scripting. Come on in: the water's just fine...

Redirection

One of the greatest capabilities of the shell, and one of the truly great inventions of Unix (then reimplemented in Linux), is file redirection. But redirection offers quite a bit more power than you may be used to.

The most basic redirection is to use > to point the output to a specified filename. This looks like:

ls > my-files

This saves the output of the ls command to a file called "my-files". If you want to add something else to the file, this is known in Linux circles as appending content, and that's done by using >> rather than just a single angle bracket:

date >> my-files

This adds the output of the date command to the existing contents of "my-files". Be careful, though; it doesn't know how to add a few blank lines between content blocks or anything else. It's crude and direct, literally opening up the file and streaming in the content.

You can also redirect input, which is very useful too. For example, to find out how many words are in the new file, use the wc command with the "-w" flag:

wc -w < my-files
48

This shows that there are 48 "words" in the file (in this case digits with spaces around them are also counted as words, so it's not that useful a result in this particular instance).

More Sophisticated Uses

A more sophisticated use of shell redirection is to use what's known as a here document, which is best explained by demonstrating it:

wc -w << ENDMARK
This is a test of what I would
ordinarily have in a file but am
instead streaming from within
this script.
ENDMARK
21

Can you see what's happened here? I've used a here document to make an instant file that I've then fed to the wc command. Once the command has processed its input, though, the file contents vanish and nothing's left behind.

This proves to be a tremendously useful technique in shell scripting because we can, for example, create 3–5 line mini-scripts for specific Linux commands within a shell script, so we don't have to worry about a bunch of files all being available when they can easily be created on-the-fly.

These are the basics of file redirection, at least until we consider the difference between stdin, stdout, and stderr, which we'll explore in the next column. In the meantime, thanks for reading and please e-mail me with any and all questions you have or topics you'd like to see me discuss here!

More Stories By Dave Taylor

Dave Taylor, a contributing editor to Linux.SYS-CON.com, has been involved with the Linux and Unix community since 1980 and has written a number of best-selling Unix books. Currently, he writes, teaches, and works as a management consultant to tech startups, along with his new venture, Ask Dave Taylor!, www.askdavetaylor.com and his personal blog is www.blog.Linux.SYS-CON.com. To contact Dave, please go to /www.intuitive.com/contact.shtml.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Byron Rendar 07/29/04 11:00:54 AM EDT

Dave's use of redirecting input was rather weak. In his example with wc -w there was no need to use input redirection, so why bother to type the extra "<".
In truth input redirection is used much less often than output redirection, and mainly with tr or mail[x].
It is true that in a script or pipeline if all you want is the output, without the filename, then < with wc makes sense.