Variables

walden systems, geeks corner, programming, languages, bash, tuples, lists, string, variables, developer, scripting
Bash is the GNU Project's shell. Bash is the Bourne Again SHell. Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use. In addition, most sh scripts can be run by Bash without modification.

Variables work as temporary storage for any programming language. Variables are used to store specific data. The most commonly used data type of variables are integer, string, float, double and Boolean. The data type of any variable has to be defined at the time of variable declaration for strongly type programming languages. But BASH is a weakly typed programming language that does not require to define any data type at the time of variable declaration. BASH variables can be used from terminal or on any BASH file.

You don't have to use any special character before the variable name at the time of setting value in BASH like other programming languages. But you have to use "$" symbol before the variable name when you want to read data from the variable. The following example show how you can set and get data from a variable in a terminal.

Setting variable
l_name = "John Doe"
will store the value "John Doe" in a variable called l_name

Getting the value stored in the variable
echo $l_name
Will output John Doe


Special variables in BASH



BASH has system reserved variable names that have special meaning. Below is a list of reserved variable names.

$0
The name of the Bash script

$1 - $9
The first 9 arguments to the Bash script.

$#
The number of arguments that were passed to the Bash script.

$@
All the arguments supplied to the Bash script.

$?
The exit status of the most recently run process.

$$
The process ID of the current script.

$USER
The username of the user running the script.

$HOSTNAME
The hostname of the machine the script is running on.

$SECONDS
The number of seconds since the script was started.

$RANDOM
Returns a different random number each time is it referred to.

$LINENO
Returns the current line number in the Bash script.


Variable Operations

In BASH, you can assign strings to variables by using either the single quote or double quote. There is a difference between the two in that BASH does not evaluate what is between single quotes as opposed to double quotes where bash evaluates what is between them. The sample code below will clarify this:

1    f_name = "John"
2    l_name = 'Doe'
3    full_name = "$f_name $l_name"
4    full_name2 = '$f_name $l_name'

After the execution of line 1, f_name will hold the string John
After the execution of line 2, l_name will hold the string Doe
After the execution of line 3, full_name will hold the string John Doe
After the execution of line 4, full_name2 will hold the string $f_name $l_name
The single quote treats what is inside literally



In BASH, numeric values are taken as strings. So no arithmetic operation can be done by normal expression and it just combines the numeric values. In order to do arithmetic operations, you will have to write the expression with double first bracket and without the "$" in front of the variable name. Here are some examples of how to execute arithmetic operations.

    num1=100
    num2=$num1+10

num2 will hod the following string "100+10"

    num1=100
    ((num2=num1+10))

num2 will hold the string "110"