Bash Variables

Variable is a temporary store for a simple piece of information, making it easier to use and manage in scripts.
Tow actions that we do with variables is setting a value for the variable, or reading its value .Almost of us have used variables before specially in maths class , for example : giving  X = 5 ( X is the variable , X = 5 is setting  a value for that variable ) ,
So , X * 2 = 10 ( we read the value of the variable and used it ).
That was variables in simple way.

For Bash scripting, you are not limited for numbers as variables’ value , you can use any data type . A variable in bash can contain a number, a character, a string of characters.

01. Using variables :

To define a variable use a meaningful name.

Variable name must begin with alphanumeric character or underscore character (_), followed by one or more alphanumeric or underscore characters. Valid shell variable examples: test , _test , test1 , BUT NOT 9test or *test1 .

Variables names are case-sensitive, just like filenames.

Do not put spaces on either side of the equal sign when assigning value to variable. For example, the following is valid variable declaration: x=5 , y=100 , m=”hello world” , N=’Welcome to Egypt’ ,…

You can define a NULL variable as follows (NULL variable is variable which has no value at the time of definition): like:  x= , y=”” .

To use a variable use ( $ ) dollar-sign followed by the variable name: x=”hello world” ; echo $x

echo is command-line and script tool used to print a line.

[root@server02 ~]# x="Welcome to Egypt"
[root@server02 ~]# echo $x
Welcome to Egypt
[root@server02 ~]#

and to do that in a script file (using any editor , i use vim , .sh file extension but not necessary ) :

vim var.sh
#!/bin/bash
x="Welcome to Egypt"
echo $x

#!/bin/bash : is called shebang and used to define the path to the bash , as there is many bash/shell types in Linux .

now allow to execute that script and run it.

[root@server02 ~]# chmod +x var.sh 
[root@server02 ~]# ./var.sh 
Welcome to Egypt
[root@server02 ~]#
02. Special variables :

There are a few other variables that the system sets for you to use as well.

  • $0 – The name of the Bash script.
  • $1 – $9 – The first 9 arguments to the Bash script. (As mentioned above.)
  • $# – How many arguments 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.
  • $PATH – Returns the path for executables in bash
  • There are more others , you will know them as you need them.
03. Bash String quotes :

To set a value with more than one word, you must use quotes [ single ( ‘ ) , or double ( ” ) ].
x=”Welcome to Egypt” , or x=’Welcome to Misr’

Single quotes ( ‘ ) : can’t use variables’ values.

Double quotes ( ” ) : can execute and use variables’ value.

[root@server02 ~]# x=5
[root@server02 ~]# y='x is equal $x'
[root@server02 ~]# echo $y
x is equal $x
[root@server02 ~]# y="x is equal $x"
[root@server02 ~]# echo $y
x is equal 5
[root@server02 ~]#
04. Command substitution :

Taking the output of a command or program (what would normally be printed to the screen) and save it as the value of a variable. To do this we place it within brackets, preceded by a $ sign.

[root@server02 ~]# x=$(ls -l)
[root@server02 ~]# echo $x
total 20 
drwxr-xr-x. 2 root root 6 May 5 14:27 test 
-rwxr-xr-x 1 root root 41 May 6 13:10 var 
-rw-r--r-- 1 root root 36 May 6 13:23 var1.sh 
-rwxr-xr-x 1 root root 41 May 6 13:08 var.sh
[root@server02 ~]#

05. Example :

vim var1.sh

Paste this into our script :

#!/bin/bash
echo "Script Name is : $0 "
echo "The first argument is : $1 "
echo "The user who execute this is : $USER , on host : $HOSTNAME "
echo " The process ID of this script : $$ "
echo "A Random number : $RANDOM "

add execute permission and run it once without an argument and once with an argument.

[root@server02 ~]# chmod +x var1.sh 
[root@server02 ~]# ./var1.sh 
Script Name is : ./var1.sh 
The first argument is :  
The user who execute this is : root , on host : server02 
The process ID of this script : 3549 
A Random number : 1894 

[root@server02 ~]# ./var1.sh hello
Script Name is : ./var1.sh 
The first argument is : hello 
The user who execute this is : root , on host : server02 
The process ID of this script : 3550 
A Random number : 30668 
[root@server02 ~]#

Congratulation , now you can write a simple script and use variables.

That is it , i hope it was simple , thanks for joining me.
Enjoy !.

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s