Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

4 Command Arguments: Echo $ (Path)

The document discusses variables and arguments in C shell scripts. It explains that variables can be accessed with a leading $ or ${variable} and describes using #$ to get the number of elements in a variable's list. Command arguments are accessible as $argv[n] or $n and $#argv provides the number of arguments. An example script is given that uses arguments to swap two files.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

4 Command Arguments: Echo $ (Path)

The document discusses variables and arguments in C shell scripts. It explains that variables can be accessed with a leading $ or ${variable} and describes using #$ to get the number of elements in a variable's list. Command arguments are accessible as $argv[n] or $n and $#argv provides the number of arguments. An example script is given that uses arguments to swap two files.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

will output the value of the variable $path.

Or you can access the variable by enclosing the variable name in curly brace characters, and then prefixing it with a $. The command
echo ${path}

would have the same result as the last example. The second method is used when something is to be appended to the contents of the variable. For example, consider the commands
set fname = prog1 rm ${fname}.c

These would delete the file `prog1.c'. To see how many elements are in a variable's list, we prefix with a # then a $. The command
echo $#V

above would print 3 to the screen, while


echo $#path

would reveal the number of directories in your search path. The @ command can be used for computations. For example, if you have shell variables $X and $Y, you can set a third variable $Z to their sum by
@Z = $X + $Y

4 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.

You might also like