Any programming language need variables, loops, arrays, …
So let’s start with discussing variables in Shell Script:
In Linux (Shell), there are two types of variables:
System variables - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS
CAUTION: Never try to change their values it may result in unexpected results.
User defined variables (UDV) - Created and maintained by user. This type of variable defined in lower letters
| System Variable | Meaning |
| BASH=/bin/bash | Our shell name |
| COLUMNS=80 | No. of columns for our screen |
| HOME=/home/vivek | Our home directory |
| LINES=25 | No. of columns for our screen |
NOTE that
You can print any of the above variables contains as follows:
| echo $HOME |
How to define User defined variables (UDV)
Syntax: variable_name=value
'value' is assigned to given 'variable name' and Value must be on right side = sign.
Example:
| # NOTE: value must be on right side of = sign.
x=10 # correct method 10=x # incorrect method city= Delhi # incorrect method since there are spaces |
Rules for giving variable name (Both UDV and System Variable)
(1) Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character. For e.g.
| HOME
SYSTEM_VERSION city |
(2) Don't put spaces on either side of the equal sign when assigning value to variable.
| no=10
#correct
no =10 #incorrect no= 10 # incorrect no = 10 # incorrect |
(3) Variables are case-sensitive, just like filename in Linux. For e.g.
| no=10
No=11 NO=20 |
(4) You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition)
| X=“” |
(5) Do not use ?,* (wildcharacters) etc, to name your variable names.
(6) Don’t use name variables with ‘system varaiables names’
| HOME=Delhi #NEVER |
Variables Arithmetic
Syntax: expr $variable1 math-operator $variable2
Examples:
| expr $x + $y
expr $x - $y expr $x / $y expr $x % $y expr $x \* $y # Warning: * is a wildcard , So remember to use ‘\’ echo `expr$x + $y` |
For the last statement note the following points
(1) First, before expr keyword we have used ` (back quote) sign not the (single quote i.e. ') sign.
{Back quote is generally found on the key under tilde (~) on PC keyboard OR to the above of TAB key.}
(2) Second, expr also ends with ` i.e. back quote.
(3) Here expr 6 + 3 is evaluated to 9, then echo command prints 9 as sum
| echo “expr $x + $y”
echo ‘$x + $y’ # it will not be evaluated |
Try this:-
Variable Assignment and Substitution
| a=375
hello=$a echo $hello #---------------------------------------------------------------- # Quoting a variable preserves whitespace. hello="A B C D" #------------------------------------------------------------ #It is permissible to set multiple variables on the same #line, if separated by white space. var1=variable1 var2=variable2 var3=variable3 #------------------------------------------------------------ |
“=” Assignment operator
| # In a 'read' statement (also a type of assignment)
echo −e "Enter \"a\"\c:" # \c will read the input in same line # Press enter afterwards read a echo "The value of \"a\" is now $a." echo exit 0 |
Variable Assignment, plain and fancy
| a=23 # Simple case
echo $a b=$a echo $b # Now, getting a little bit fancier (command substitution). a=`echo Hello!` # Assigns result of 'echo' command to 'a' echo $a # Note that using an exclamation mark (!) in command substitution # will not work from the command line, #+ since this triggers the Bash "history mechanism." # Within a script, however, the history functions are #disabled. a=`ls −l` # Assigns result of 'ls−l' command to 'a' echo $a # Unquoted, however, removes tabs and newlines. echo echo "$a" # The quoted variable preserves whitespace. |
Bash Variables Types
Unlike many other programming languages, Bash does not segregate its variables by "type". Essentially, Bash
variables are character strings, but, depending on context, Bash permits integer operations and comparisons on
variables. The determining factor is whether the value of a variable contains only digits.
| #!/bin/bash
#string_integer.sh # Declaring Integers and Strings a=5 a=`expr $a + 1` echo $a a=hello a=`expr $a + 1` echo "$a" a=`expr $a + 1` echo "$a" exit 0 |
Run your script as $bash ./ string_integer.sh
Did you notice the difference?
