Bash VARIABLES



  • We cannot use bash variables without having the proper information (syntax, data types, types, working) 
  • Variables are the containers which store data or a useful piece of information as the value inside them. 

Syntax:
Variable_name=value;

  • Single Quote ('') helps to treat every character.
  • Double Quote ("") helps to do the substitution.

  • To know the list of these variables in your system, type the commands set, env, and printenv on the command line terminal as follows:

[haritaraka12078@cxln4 ~]$ set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()


--------------------------------------------------------------------------------------------------------------------------
- System defined variables
-------------------------------------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ cat var.sh
#! /bin/bash
# Bash System-defined Variables
echo $HOME # Home Directory
echo $PWD # current working directory
echo $BASH # Bash shell name
echo $BASH_VERSION # Bash shell Version
echo $LOGNAME # Name of the Login User
echo $OSTYPE # Type of OS


[haritaraka12078@cxln4 ~]$ ./var.sh
/home/haritaraka12078
/home/haritaraka12078
/bin/bash
4.2.46(2)-release
haritaraka12078
linux-gnu


-------------------------------------------------------------------------------------------------------------------------
- user defined variables
-------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ a=11000
[haritaraka12078@cxln4 ~]$ echo $a
11000


[haritaraka12078@cxln4 ~]$ readonly a
[haritaraka12078@cxln4 ~]$ a=1000
-bash: a: readonly variable


[haritaraka12078@cxln4 ~]$ unset a
-bash: unset: a: cannot unset: readonly variable

[haritaraka12078@cxln4 ~]$ echo $a




Examples:1

[haritaraka12078@cxln4 ~]$ cat uservar.sh
#! /bin/bash
# User-Defined Variables
name=Peter
ROLL_NO=5245325
echo "The student name is $name and his Roll number is $ROLL_NO."


[haritaraka12078@cxln4 ~]$ chmod 777 uservar.sh

[haritaraka12078@cxln4 ~]$ ./uservar.sh
The student name is Peter and his Roll number is 5245325.




Example 2

[haritaraka12078@cxln4 ~]$ cat>var2.sh
#! /bin/bash
#Bash Variables
USER_NAME=XYZ
echo Hey there! $USER_NAME is any user currently working on the directory $PWD with Bash Shell Version $BASH_VERSION.

[haritaraka12078@cxln4 ~]$ chmod 777 var2.sh

[haritaraka12078@cxln4 ~]$ ./var2.sh
Hey there! XYZ is any user currently working on the directory /home/haritaraka12078 with Bash Shell Version 4.2.46(2)-release.



-------------------------------------------------------------------------------------------------------------------------
-combine strings
-------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ var1="my name is hari taraka prabhu"
[haritaraka12078@cxln4 ~]$ var2="and i'm working as software engineer"


[haritaraka12078@cxln4 ~]$ echo $var1 $var2
my name is hari taraka prabhu and i'm working as software engineer



Example:1

[haritaraka12078@cxln4 ~]$ cat>comb.sh
#! /bin/bash
#combine strings
str_v1="you are combining two strings"
x="by using shell script"
echo $str_v1 $x


[haritaraka12078@cxln4 ~]$ ./comb.sh
you are combining two strings by using shell script


-------------------------------------------------------------------------------------------------------------------------
How to use command line arguments?
-------------------------------------------------------------------------------------------------------------------------
In a Bash Shell, they are used with the reference of the following default-parameters or the special variables.

$0--------- specifies the name of the script to be invoked.
$1-$9 ------------stores the names of the first 9 arguments or can be used as the arguments' positions.
$# ---------specifies the total number (count) of arguments passed to the script.
$* ----------stores all the command line arguments by joining them together.
$@ -------------stores the list of arguments as an array.
$? ------------specifies the process ID of the current script.
$$ ----------specifies the exit status of the last command or the most recent execution process.
$! ---------------shows ID of the last background job.


Example 1:

[haritaraka12078@cxln4 ~]$ cat>array.sh
#!/bin/bash
args=("$@")
echo ${args[0]} ${args[1]} ${args[2]} ${args[3]}


[haritaraka12078@cxln4 ~]$ ./array.sh hari taraka prabhu
hari taraka prabhu


[haritaraka12078@cxln4 ~]$ ./array.sh 100 20 300
100 20 300