Variables in Shell Programing

Variables as the name says , they can vary , but rather than defining them in way we understand, there is a more common way , rather more general and accepted definition:

Variables are sequences of letters, digits, or underscores beginning with a letter or underscore. To get the contents of a variable you must perpend the name with a $.

Ex : my_name_007 = "Bond"

Assignment :
Giving values to variable is very simple , it's doing using the "=" symbol.
Ex :user = "hemanth"

Exporting : Children can't use the variable , unless they are exported ,you get to know this when you start of with coding.
Ex : PATH = /usr/bin/hmh ; export PATH;

Use $variable or ${variable} to reference the value.
Ex: $name_007 ===> here the variable is name_007
Ex: ${name}_007 ==> here name is concatenated with _007

Conditional Reference
If x has been set, use it's value, else use y.
If the x has been set and is not null, use it's value, else use word.

If x is set use it's value, else print out y and exit. Positional variables : Command line variables of the shell can be retrieved as : $0,$1,$2.......
$#  ==>The required argument.
$*, $@ ==>Th
e number of arguments.

Special Variables

$$ ==> Current PID {Process ID}.

$? ==> The exit status of the last command.

Quotes/Special Characters :

Characters to terminate words : ; & ( ) | ^ new-line space tab.
Single Quotes : all  characters  are  quoted {including the backslash!! ' '}.

Double Quotes : variable substitution , no file name generation { regex wont work , " "}.

Back Quotes : execute the command and substitute the output. { also called back ticks ` `}.

Share this