Working with Parameters in a Shell Script (.sh)
In a bash script, you can handle parameters passed from the command line. Below are the main parameters and how to use them in your script:
1. Accessing Parameters
Parameters are accessible using positional variables inside the script:
$0: The script’s name.$1,$2, …,$N: The values of the passed arguments.$#: The number of arguments provided.$@: All arguments as a list (each argument is treated as a separate string).$*: All arguments as a single string.$?: The return code of the last command.$$: The process ID (PID) of the running script.$_: The last argument of the previous command.
Basic Script Example with Parameters
#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Number of arguments: $#"
# Checking if an argument was provided
if [ $# -lt 1 ]; then
echo "No argument was provided."
exit 1
fi
# Iterating over all arguments
for arg in "$@"; do
echo "Argument: $arg"
done
2. Using Flags with Arguments
For more sophisticated scripts, you can add flags like -h for help or -f for specifying a file:
#!/bin/bash
while getopts ":f:h" option; do
case $option in
f) # Parameter -f is passed
file=$OPTARG
echo "File: $file"
;;
h) # Displays help
echo "Usage: $0 -f [file] -h [help]"
exit 0
;;
\?) # Invalid flag
echo "Invalid option: -$OPTARG"
exit 1
;;
esac
done
3. Complete Example: Processing Arguments and Flags
Here’s an example script that accepts options and parameters and validates input:
#!/bin/bash
function show_help {
echo "Usage: $0 -n [name] -a [age]"
exit 1
}
# Check if any arguments were passed
if [ $# -eq 0 ]; then
show_help
fi
while getopts "n:a:h" opt; do
case $opt in
n)
name=$OPTARG
;;
a)
age=$OPTARG
;;
h)
show_help
;;
\?)
echo "Invalid option: -$OPTARG" >&2
show_help
;;
🙂
echo "Option -$OPTARG requires an argument." >&2
show_help
;;
esac
done
# Validate input
if [ -z "$name" ] || [ -z "$age" ]; then
show_help
fi
echo "Name: $name"
echo "Age: $age"
4. Making the Script Executable
After writing the script, you need to make it executable using the chmod command. Here’s how to do it:
chmod +x your_script.sh
This command grants execution permissions to the script. You can then run the script using:
./your_script.sh -n "John" -a 25
Code Section
#!/bin/bash
# Basic script with parameters
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Number of arguments: $#"
if [ $# -lt 1 ]; then
echo "No argument was provided."
exit 1
fi
for arg in "$@"; do
echo "Argument: $arg"
done
#!/bin/bash
# Script with flags and argument processing
while getopts ":f:h" option; do
case $option in
f)
file=$OPTARG
echo "File: $file"
;;
h)
echo "Usage: $0 -f [file] -h [help]"
exit 0
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
esac
done
#!/bin/bash
# Complete script example with input validation
function show_help {
echo "Usage: $0 -n [name] -a [age]"
exit 1
}
if [ $# -eq 0 ]; then
show_help
fi
while getopts "n:a:h" opt; do
case $opt in
n)
name=$OPTARG
;;
a)
age=$OPTARG
;;
h)
show_help
;;
\?)
echo "Invalid option: -$OPTARG" >&2
show_help
;;
🙂
echo "Option -$OPTARG requires an argument." >&2
show_help
;;
esac
done
if [ -z "$name" ] || [ -z "$age" ]; then
show_help
fi
echo "Name: $name"
echo "Age: $age"
# Making the script executable
chmod +x your_script.sh

Leave a comment