Basic Linux Tutorial
Computer Tutorial

Variable in Shell Scripting

Variable in Shell Scripting

Variables can be set and used in the same way you have used them on the command line. Any variable that is set during the execution of a shell program is not visible after the execution is finished. Shell programs can read environment variables and can also modify their values for the duration of the execution of the program. Variables are also a useful way of passing data to shell programs. Let us see another program where we have set two variables TAB and FUR. These variables are then displayed using the echo command.

Example of a simple shell script using variables.


echo   "Use of Variables"
echo   "----------------"
TAB = table
FUR = furniture
echo   "The $TAB is an example of $FUR"

When this program is executed the results are :

Use of Variables
The table is an example of furniture

Comments in a Shell Program

All comments start with a pound sign (#) except for the special combination used in the first line for specifying the subshell. A comment can be placed anywhere in the file. If a line starts with the "#" sign, all of the line is treated as a comment. If the "#" sign is placed somewhere else in a line, anything after that sign is considered a comment. If the line contains a command echo and after that a comment string. The command is executed but the comment is ignored.

Example :
# This is a comment