Variable Scope and Special Variables
In this final step, we will explore variable scope in Bash and learn about special variables that Bash provides.
Understanding Variable Scope
In Bash, variables can have different scopes, which determine where they can be accessed:
- Global variables: These are accessible throughout the entire script
- Local variables: These are accessible only within a specific function or block
Let us create a script to demonstrate variable scope:
- Create a new file named
variable_scope.sh
:
#!/bin/bash
## Global variable
global_var="I am a global variable"
## Function that demonstrates variable scope
demo_scope() {
## Local variable
local local_var="I am a local variable"
## Access global variable
echo "Inside function: global_var = $global_var"
## Access local variable
echo "Inside function: local_var = $local_var"
## Modify global variable
global_var="Global variable modified"
}
## Main script
echo "Before function call: global_var = $global_var"
## This will fail because local_var doesn't exist yet
echo "Before function call: local_var = $local_var"
## Call the function
demo_scope
## After function call
echo "After function call: global_var = $global_var"
## This will fail because local_var is only accessible within the function
echo "After function call: local_var = $local_var"
- Save the file and make it executable:
chmod +x variable_scope.sh
- Run the script:
./variable_scope.sh
You should see output similar to:
Before function call: global_var = I am a global variable
Before function call: local_var =
Inside function: global_var = I am a global variable
Inside function: local_var = I am a local variable
After function call: global_var = Global variable modified
After function call: local_var =
Notice that:
- The global variable
global_var
is accessible both inside and outside the function
- The local variable
local_var
is only accessible within the function
- Changes to the global variable persist after the function ends
Working with Special Variables
Bash provides several special variables that hold specific information. Let us explore some of them:
- Create a new file named
special_variables.sh
:
#!/bin/bash
## Special variables demonstration
echo "Script name: $0"
echo "Process ID: $$"
echo "Number of arguments: $#"
echo "All arguments: $@"
echo "Exit status of last command: $?"
echo "Current user: $USER"
echo "Current hostname: $HOSTNAME"
echo "Random number: $RANDOM"
## Function to demonstrate positional parameters
show_params() {
echo "Function received $## parameters"
echo "Parameter 1: $1"
echo "Parameter 2: $2"
echo "Parameter 3: $3"
}
## Call the function with parameters
echo -e "\nCalling function with parameters:"
show_params apple banana cherry
## Demonstrate positional parameters to the script
echo -e "\nScript positional parameters:"
echo "Parameter 1: $1"
echo "Parameter 2: $2"
echo "Parameter 3: $3"
- Save the file and make it executable:
chmod +x special_variables.sh
- Run the script with some arguments:
./special_variables.sh arg1 arg2 arg3
You should see output similar to:
Script name: ./special_variables.sh
Process ID: 12345
Number of arguments: 3
All arguments: arg1 arg2 arg3
Exit status of last command: 0
Current user: labex
Current hostname: labex
Random number: 23456
Calling function with parameters:
Function received 3 parameters
Parameter 1: apple
Parameter 2: banana
Parameter 3: cherry
Script positional parameters:
Parameter 1: arg1
Parameter 2: arg2
Parameter 3: arg3
Environment Variables
Environment variables are special variables that affect the behavior of the shell and programs. Let us create a script to explore them:
- Create a new file named
environment_variables.sh
:
#!/bin/bash
## Display common environment variables
echo "HOME directory: $HOME"
echo "Current user: $USER"
echo "Shell: $SHELL"
echo "Path: $PATH"
echo "Current working directory: $PWD"
echo "Terminal: $TERM"
echo "Language: $LANG"
## Creating and exporting a new environment variable
echo -e "\nCreating a new environment variable:"
MY_VAR="Hello from environment variable"
export MY_VAR
echo "MY_VAR = $MY_VAR"
## Demonstrate that child processes inherit environment variables
echo -e "\nAccessing environment variable from a child process:"
bash -c 'echo "Child process sees MY_VAR = $MY_VAR"'
## Remove the environment variable
echo -e "\nRemoving the environment variable:"
unset MY_VAR
echo "MY_VAR = $MY_VAR (should be empty)"
- Save the file and make it executable:
chmod +x environment_variables.sh
- Run the script:
./environment_variables.sh
You should see output showing various environment variables and demonstrating how they can be created, accessed, and removed.
This step has shown you how variable scope works in Bash and introduced you to special variables that provide useful information within your scripts.