-->
Page 415
If you want to assign the value of var to the variable lcount, you can do that as follows:
Command | Environment |
lcount=$var pdksh and bash set lcount = $var tcsh
It is possible to write a shell script that takes a number of parameters at the time you invoke it from the command line or from another shell script. These options are supplied to the shell program by Linux as positional parameters. The positional parameters have special names provided by the system. The first parameter is stored in a variable called 1 (number 1) and can be accessed by using $1 within the program. The second parameter is stored in a variable called 2 and can be accessed by using $2 within the program, and so on. It is possible to omit one or more of the higher numbered positional parameters while invoking a shell program. For example, if a shell program mypgm expects two parameterssuch as a first name and a last namethen you can invoke the shell program with only one parameter, the first name. However, you cannot invoke it with only the second parameter, the last name.
Here's a shell program called mypgm1, which takes only one parameter (a name) and displays it on the screen:
#Name display program if [ $# == 0] then echo "Name not provided" else echo "Your name is "$1
In pdksh and bash, if you execute mypgm1 as follows:
. mypgm1
you will get the output:
Name not provided
However, if you execute mypgm1 as follows:
. mypgm1 Sanjiv
then you will get the following output:
Your name is Sanjiv
The shell program mypgm1 also illustrates another aspect of shell programming, the built-in variables. In mypgm1, the variable $# is a built-in variable and provides the number of positional parameters passed to the shell program.
Page 416
Built-in variables are a special type of variable that Linux provides to you. These variables can be used to make decisions within a program. You cannot modify the values of these variables within the shell program.
Some of these variables are
$# | Number of positional parameters passed to the shell program |
$? | Code of the last command or shell program executed within the shell program |
$0 | The name of the shell program |
$* | A single string of all arguments passed at the time of invocation of the shell program |
To show these built-in variables in use, here is a sample program called mypgm2:
#my test program echo "Number of parameters is "$# echo "Program name is "$0 echo "Parameters as a single string is "$*
In pdksh and bash, if you execute mypgm2 from the command line, as follows:
. mypgm2 Sanjiv Guha
you will get the following result:
Number of parameters is 2 Program name is mypgm2 Parameters as a single string is Sanjiv Guha
Some characters have special meaning to Linux shells, so using them as part of variable names or strings will cause your program to behave incorrectly. If a string contains such special characters, then you also have to use escape characters to indicate that the special characters should not be treated as special characters.
Some of these special characters are shown in the following table.
Character | Explanation |
$ | Indicates the beginning of a shell variable name |
| | Pipes standard output to the next command |
# | Starts a comment |
& | Executes a process in the background |
? | Matches one character |
Page 417
Character | Explanation |
* | Matches one or more characters |
> | Output redirection operator |
< | Input redirection operator |
` | Command substitution (the backquote or backtickthe key above the Tab key) |
>> | Output redirection operator (to append to a file) |
[ ] | Lists a range of characters |
[a-z] | Means all characters a through z |
[a,z] | Means characters a or z |
. filename | Executes the file filename |
Space | Delimiter between two words |
There are a few special characters that deserve special note. They are the double quotes ("), the single quote (`), the backslash (\), and the backtick (`). They are discussed in the following sections.
If a string contains embedded spaces, you can enclose the string in double quotes (") so that the shell interprets the whole string as one entity instead of more than one. For example, if you assigned the value of abc def (abc followed by one space followed by def) to a variable called x in a shell program as follows:
Command | Environment |
x=abc def pdksh and bash set x = abc def tcsh
you would get an error as the shell would try to execute def as a separate command. What you need to do is surround the string in double quotes as follows:
Command | Environment |
x="abc def" pdksh and bash set x = "abc def" tcsh
The double quotes resolve all the variables within the string. Here is an example for pdksh and bash:
var ="test string" newvar="Value of var is $var" echo $newvar
Page 418
Here is the same example for tcsh:
set var = "test string" set newvar = "Value of var is $var" echo $newvar
If you execute a shell program containing these three lines, you will get the following result:
Value of var is test string
You can use the single quote (`) to surround a string in order to stop the shell from resolving a variable. In the following example, the double quotes in the preceding example have been changed to single quotes.
pdksh and bash: var ='test string' newvar='Value of var is $var' echo $newvar tcsh: set var = `test string' set newvar = `Value of var is $var' echo $newvar
If you execute a shell program containing these three lines, you will get the following result:
Value of var is $var
As you can see, the variable var did not get resolved.
You can use a backslash (\) before a character to stop the shell from interpreting the following character as a special character. Say you want to assign a value of $test to a variable called var. If you use the following command:
Command | Environment |
var=$test pdksh and bash set var = $test tcsh
then a null value will be stored in var. This is because the shell will interpret $test as the value of the variable test, and as test has not been assigned any value, var will contain null. You should use the following command to correctly store $test in var: