home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Unix Power ToolsUnix Power ToolsSearch this book

35.25. The Bourne Shell set Command

[Most of this article, except IFS and --, also applies to the C shell. -- JP]

You can pass options and arguments to a shell as you start it, as in:

sh -v file1 file2

and also when a script is invoked with #!. The set command lets you set command-line parameters, including most[112] shell options, after you've started the shell. This simple idea has more uses than you might realize.

[112]Some options for some shells can be set only from the command line as the shell is invoked. Check the shell's manual page.

35.25.1. Setting Options

The Bourne shell command line can have options like -e (exit if any command returns nonzero status). It can also have other arguments; these are passed to shell scripts. You can set new command-line parameters while you're typing interactive commands (at a shell prompt) or in a shell script.

To reset the command-line parameters, just type set followed by the new parameters. For example, to ask the shell to show expanded versions of command lines after you type them, set the -v (verbose) option (Section 27.15):

$ set -v
$ mail $group1 < message
mail andy ellen heather steve wilma < message
$ mail $group2 < message
mail jpeek@jpeek.com randy@xyz.edu yori@mongo.medfly.com < message
$ set +v

On many Bourne shells, typing set +v cancels the v option. On other (mostly early) shells, there's no + feature. To work around that problem, you could start a subshell (Section 24.4) with sh -v, run the commands there, then exit the subshell to cancel the verbose option.

35.25.3. (Avoiding?) set with No Arguments

If you type set by itself with no arguments, it will show a list of all currently set shell variables. In newer Bourne-type shells, it also shows shell functions (Section 29.11) and other shell settings.

This can cause you grief if you accidentally don't pass arguments to set in the middle of a script, and screenfuls of variables spew onto the user's screen. For example, your script runs set 'users' from a cron (Section 25.2) job, in the middle of the night when no one is logged on. The users command returns an empty string, so set gets no arguments, so it outputs a long list of junk.

The standard workaround for this problem is to always use a dummy first parameter -- typically, a single x -- when you're setting parameters. Then use shift (Section 35.22) to shift away the x, leaving the other parameters (possibly none). For example:

set x `users`
shift

35.25.4. Watch Your Quoting

Because the shell parses and scans the new parameters before it stores them, wildcards (Section 33.2) and other special characters (Section 27.17) will be interpreted, so watch your quoting (Section 27.12). You can take advantage of this to parse lines of text into pieces that aren't separated with the usual spaces and TABs -- for instance, a line from a database with colon-separated fields -- by setting the IFS (Section 36.23) variable before the set command.

If you want to save any special quoting on the original command line, be careful: the quoting will be lost unless you're clever. For example, if $1 used to be John Smith, it will be split after it's restored: $1 will have John and $2 will be Smith. A better solution might be to use a subshell (Section 43.7) for the part of the script where you need to reset the command-line parameters:

# reset command-line parameters during subshell only:
(set some new parameters
   ...do something with new parameters...
)
# original parameters aren't affected from here on...

35.25.5. Can't Set $0

One last note: set won't set $0, the name of the script file.

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.