Local variable

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← Pass arguments into a functionHomeReturning from a function →
  • By default all variables are global.
  • Modifying a variable in a function changes it in the whole script.
  • This can be result into problem. For example, create a shell script called fvar.sh:
#!/bin/bash
create_jail(){
   d=$1  
   echo "create_jail(): d is set to $d"
}
 
d=/apache.jail
 
echo "Before calling create_jail  d is set to $d"
 
create_jail "/home/apache/jail"
 
echo "After calling create_jail d is set to $d"

Save and close the file. Run it as follows:

chmod +x fvar.sh
./fvar.sh

Sample outputs:

Before calling create_jail  d is set to /apache.jail
create_jail(): d is set to /home/apache/jail
After calling create_jail d is set to /home/apache/jail

local command

  • You can create a local variables using the local command and syntax is:
local var=value
local varName

OR

function name(){
   local var=$1
   command1 on $var
}
  • local command can only be used within a function.
  • It makes the variable name have a visible scope restricted to that function and its children only. The following is an updated version of the above script:
#!/bin/bash
# global d variable
d=/apache.jail
 
# User defined function
create_jail(){
   # d is only visible to this fucntion
   local d=$1  
   echo "create_jail(): d is set to $d"
}
 
echo "Before calling create_jail  d is set to $d"
 
create_jail "/home/apache/jail"
 
echo "After calling create_jail d is set to $d"

Sample output:

Before calling create_jail  d is set to /apache.jail
create_jail(): d is set to /home/apache/jail
After calling create_jail d is set to /apache.jail

Example

In the following example:

  • The declare command is used to create the constant variable called PASSWD_FILE.
  • The function die() is defined before all other functions.
  • You can call a function from the same script or other function. For example, die() is called from is_user_exist().
  • All function variables are local. This is a good programming practice.
#!/bin/bash
# Make readonly variable i.e. constant variable
declare -r PASSWD_FILE=/etc/passwd
 
#
# Purpose: Display message and die with given exit code
# 
die(){
        local message="$1"
        local exitCode=$2
        echo "$message"
        [ "$exitCode" == "" ] && exit 1 || exit $exitCode
}
 
#
# Purpose: Find out if user exits or not
#
does_user_exist(){
        local u=$1
        grep -qEw "^$u" $PASSWD_FILE && die "Username $u exists."
}
 
#
# Purpose: Is script run by root? Else die..
# 
is_user_root(){
  [ "$(id -u)" != "0" ] && die "You must be root to run this script" 2
}
 
#
# Purpose: Display usage
# 
usage(){
	echo "Usage: $0 username"
	exit 2
}
 
 
[ $# -eq 0 ] && usage
 
# invoke the function is_root_user
is_user_root
 
# call the function is_user_exist
does_user_exist "$1"
 
# display something on screen
echo "Adding user $1 to database..."
# just display command but do not add a user to system
echo "/sbin/useradd -s /sbin/bash -m $1"
← Pass arguments into a functionHomeReturning from a function →