Command-Line Arguments in Shell Script

Command-line arguments are a way to pass input directly during the execution of a shell script, eliminating the need to hardcode variables or read them dynamically during runtime. These arguments are captured and processed using positional parameters and special variables.

What are Positional Parameters?

Positional parameters are variables that store command-line arguments passed to a script. These are represented by $1, $2, $3, and so on, where $1 is the first argument, $2 is the second, and so forth.

Syntax

#!/bin/bash
variable=${1}

Here:

  • ${1} represents the first command-line argument.
  • You can define multiple positional parameters similarly, such as ${2}, ${3}, etc.

Example 1: Basic Usage

#!/bin/bash
name=${1}
age=${2}

echo "Hello, my name is ${name} and my age is ${age}."

Execution

  1. Grant execute permission to the script:

    chmod +x commandlineargs.sh
    
  2. Run the script with arguments:

    ./commandlineargs.sh Gaurav 30
    

Output

Hello, my name is Gaurav and my age is 30.

Example 2: Displaying All Arguments

The following script demonstrates how to access all arguments and special variables:

#!/bin/bash
name=${1}
age=${2}

# Display all positional parameters
echo "Argument 1: ${1}"
echo "Argument 2: ${2}"

# Special Variables
echo "Script Name: ${0}"  # Name of the script
echo "Total Arguments: $#"  # Total number of arguments
echo "All Arguments (quoted): $*"  # All arguments as a single string
echo "All Arguments (individual): $@"

Special Variables

VariableDescriptionExample
${0}The name of the script itself../commandlineargs.sh
${1}, ${2} ...The positional parameters corresponding to the arguments passed to the script.${1} -> Gaurav, ${2} -> 30
$#Total number of arguments passed to the script.2
$*All the arguments passed as a single string, quoted."Gaurav 30"
$@All the arguments passed as individual words, retaining their separation."Gaurav" "30"
$?Exit status of the last executed command.0 for success, non-zero for failure.
$$Process ID of the current shell script.e.g., 8723
$PPIDParent process ID of the current shell script.e.g., 8701

Advanced Examples

Looping Through Arguments

#!/bin/bash
for arg in "$@"; do
    echo "Argument: ${arg}"
done

Execution:

./loopargs.sh Gaurav 30 Developer

Output:

Argument: Gaurav
Argument: 30
Argument: Developer

Using Total Arguments in Logic

#!/bin/bash
if [ $# -lt 2 ]; then
    echo "Error: At least 2 arguments are required."
    exit 1
fi

echo "You provided $# arguments."

Execution:

./countargs.sh Gaurav

Output:

Error: At least 2 arguments are required.

Combining Arguments

#!/bin/bash
combined="$*"
echo "Combined Arguments: ${combined}"

Execution:

./combineargs.sh Hello World Bash

Output:

Combined Arguments: Hello World Bash

Handling Special Characters in Arguments

#!/bin/bash
echo "First Argument: ${1}"

Execution:

./specialchar.sh "Hello & Welcome"

Output:

First Argument: Hello & Welcome

Best Practices

  1. Validate Input: Ensure the required number of arguments are passed before processing.
  2. Quote Variables: Always quote variables to handle spaces or special characters.
  3. Use Default Values: Provide defaults for missing arguments using parameter expansion: ${1:-default_value}.

Demo Video

Click Here for Demo Video