Using Variables in a Bash Script (.sh)
Variables in a Bash script are an essential part of making your scripts dynamic and reusable. You can store values, pass them to functions, manipulate them, and more.
1. Defining Variables
In Bash, variables are defined without a = sign surrounded by spaces. You can define a variable like this:
variable_name="value"
Here, the variable variable_name holds the string value.
Example
#!/bin/bash
# Defining a variable
home="house"
# Accessing and printing the variable
echo "The value of home is: $home"
Important Notes:
- No spaces around the
=sign. - To access a variable, you must use the
$sign before the variable name:$home.
2. Functions and Variables
Functions allow you to group blocks of code that can be reused. Variables can be declared globally or locally within functions.
Defining a Function
In Bash, functions are defined like this:
function function_name {
# Function body
}
Passing Variables to Functions
Variables can be passed to functions as arguments. Inside the function, the arguments are accessed with positional parameters like $1, $2, and so on.
Example with Functions and Variables
#!/bin/bash
# Global variable
greeting="Hello"
# Function to greet with a passed name
function greet {
local name=$1 # First argument passed to the function
echo "$greeting, $name!"
}
# Using the function with an argument
greet "John"
Here, greet "John" calls the greet function and passes "John" as the argument. Inside the function, the name variable holds "John", and the function prints "Hello, John!".
3. Modifying Variables within Functions
You can modify global variables within a function or work with local variables to avoid affecting the global scope.
Example
#!/bin/bash
# Global variable
count=10
# Function to modify the variable
function increment {
local new_count=$((count + 1)) # Local variable
echo "Inside function, new count: $new_count"
}
# Call the function
increment
# Outside the function, global count remains unchanged
echo "Outside function, count: $count"
In this example, the increment function creates a local variable new_count based on the global count variable, but it does not modify the global variable.
4. Using Environment Variables
Environment variables, like $HOME, $PATH, and $USER, are available in all processes and can be accessed in Bash scripts.
Example
#!/bin/bash
# Using the environment variable HOME
echo "Your home directory is: $HOME"
# Using the environment variable USER
echo "Logged in as: $USER"
5. Combining Variables and Arguments
You can combine variables with passed arguments to make your script more flexible.
Example
#!/bin/bash
# Global variable
location="house"
# Function with arguments and global variables
function move {
local destination=$1 # First argument passed
echo "Moving from $location to $destination"
}
# Using the function with a new destination
move "office"
Here, the move function takes one argument, and the global variable location is used in the output. The output would be: Moving from house to office.
Code Summary
#!/bin/bash
# Defining a variable
home="house"
echo "The value of home is: $home"
# Function with arguments and global variables
function greet {
local name=$1
echo "$greeting, $name!"
}
greeting="Hello"
greet "John"
# Function to modify global variable
count=10
function increment {
local new_count=$((count + 1))
echo "Inside function, new count: $new_count"
}
increment
echo "Outside function, count: $count"
# Using environment variables
echo "Your home directory is: $HOME"
echo "Logged in as: $USER"
# Combining variables and arguments
location="house"
function move {
local destination=$1
echo "Moving from $location to $destination"
}
move "office"

Leave a comment