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.
Related articles
- Perl - Downloading a URL with a script - [22nd Mar 2006]
- Perl - Pattern Matching (Regular Expressions) - [12th Jan 2006]
- Shell Scripting - Reading Variables From STDIN - [11th Jan 2006]
- Perl - Configuring and Starting the mod_perl Server - [06th Jan 2006]
- Perl - Installing mod_perl in Three Steps - [06th Jan 2006]
- Perl - mod_perl - [06th Jan 2006]
- Perl - Associative Arrays - [06th Jan 2006]
- Perl - Manipulating Arrays - [06th Jan 2006]
- Perl - Arrays - [06th Jan 2006]
- Perl - Operators - [06th Jan 2006]
- Perl - Scalar Variables - [06th Jan 2006]
- Shell Scripting - Introduction - [08th Dec 2005]
- Shell Scripting - Invoking Shell Scripts - [08th Dec 2005]
- Shell Scripting - Variables - [08th Dec 2005]
- Shell Scripting - Referencing Variables - [08th Dec 2005]
- Shell Scripting - Command Arguments - [08th Dec 2005]
- Shell Scripting - Language Constructs - [08th Dec 2005]
- Perl - Introduction - [06th Dec 2005]
- MySQL - Connecting to the server - [06th Dec 2005]
- MySQL - Introduction - [06th Dec 2005]







