9.5 Substitution

   

There are three types of substitutions used in the POSIX shell. These will be explained next .

Variable Substitution

In a variable substitution, a variable name is used with the $ sign in the command line. The value of the variable is then substituted in the command line before it is executed. For example, the echo command prints whatever it receives on the command line.

 $  echo HOME  HOME $  echo $HOME  /home/boota $ 

In the first case, the echo command printed what you typed at the command line. In the second case, echo substituted the variable $HOME and printed the value of the HOME variable.

Command Substitution

The result of another command can also be substituted in a command at the command line. Here I have used the same example of the echo command.

 $  echo date  date $  echo $(date)  Wed Oct 13 22:53:19 EDT 1999 $  echo `date`  Wed Oct 13 22:53:29 EDT 1999 $ 

In the first case, the echo command prints the word "date." In the second and third cases, the date command is executed and its result is substituted in the echo command.

Tilde Substitution

Tilde substitution is governed by the following rules.

  • A ~/ is replaced by the HOME variable.

  • A ~+ is replaced by the PWD variable.

  • A ~- is replace by the OLDPWD variable.

Where HOME points to your home directory, PWD has the value of the current directory and OLDPWD has the value of the previous working directory. A good use of tilde substitution is to go to your home directory from any other directory. See the following where you move from the /etc directory to your home directory ( /home/boota ) using tilde substitution.

 $  pwd  /etc $  cd ~/  $  pwd  /home/boota $ 

   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net