leestretton.com

You are in: > Home > Developers Guides

Shell Scripting - Command Arguments

Most commands have arguments (parameters), and these are accessible via the shell variable $argv. The first parameter will be $argv[1], the second $argv[2], and so on. You can also refer to them as $1, $2, etc. The number of such arguments (analogous to argc in the C language) is $#argv.

For example, consider the following script file, say named Swap

:
#! /bin/csh -f

set tmp = $argv[1]
cp $argv[2] $argv[1]
cp $tmp $argv[2]

This would do what its name implies, i.e. swap two files. If, say, I have files x and y, and I type

Swap x y

then the new contents of x would be what used to be y, and the new contents of y would be what used to be x.

Problem with this page? Let us know
© Lee Stretton 2005 | powered by linux powered by php 5 powered by MySQL download rss feeds powered by php 5 valid css