1. Introduction

    System Information

  2. Inter-Process Communication
  3. Signals

    Programming in Various Environments

  4. Script Programming
    1. Script Variables
    2. Test Conditions
    3. Control and Iteration
    4. Commonly used Programs
    5. Shell Capabilities
    6. Example looping script
    7. Example using Variables
    8. Example working with files
    9. Example install script
  5. C and C++ Programming
    1. POSIX System Capabilities
    2. More POSIX
    3. Threads
    4. Mutexes
    5. An example viewmod program
    6. An example serial program
  6. X Programming
  7. Debugging
  8. Credits

Linux Script Variables

When variables are used they are referred to with the $ symbol in front of them. There are several useful variables available in the shell program. Here are a few:

  • $$ = The PID number of the process executing the shell.
  • $? = Exit status variable.
  • $0 = The name of the command you used to call a program.
  • $1 = The first argument on the command line.
  • $2 = The second argument on the command line.
  • $n = The nth argument on the command line.
  • $* = All the arguments on the command line.
  • $# The number of command line arguments.

The "shift" command can be used to shift command line arguments to the left, ie $1 becomes the value of $2, $3 shifts into $2, etc. The command, "shift 2" will shift 2 places meaning the new value of $1 will be the old value of $3 and so forth.

Variable assignment

Variables are assigned in a script program as follows:

DONE=no

They are used in this manner:

while [ $DONE = no ]