Command Line Arguments in Shell Script

Command Line Arguments in Shell Script

Command Line Arguments in Shell Scripts

Command line arguments allow users to pass input directly to a script when executing it. This makes scripts dynamic and flexible, enabling them to handle varying input without modifying the code.

Accessing Command Line Arguments

In Bash, arguments passed to a script can be accessed using special variables:

VariableDescription
$0Name of the script
$1, $2, ...Positional arguments (first, second, etc.)
$#Total number of arguments passed
$@All arguments as a single string
$*All arguments as a single string (slightly different from $@)
$?Exit status of the last executed command
$$Process ID (PID) of the current script

Example: Simple Argument Handling

#!/bin/bash echo "Script Name: $0" echo "First Argument: $1" echo "Second Argument: $2" echo "Total Arguments: $#"

Execution:

bash script.sh Hello World

Output:

Script Name: script.sh First Argument: Hello Second Argument: World Total Arguments: 2

Using All Arguments ($@ vs $*)

Example:

#!/bin/bash echo "Using \$@: $@" echo "Using \$*: $*"

Execution:

bash script.sh "Hello World" Bash

Output:

Using $@: Hello World Bash Using $*: Hello World Bash

Key Difference:

  • $@: Treats each argument as a separate string.
  • $*: Treats all arguments as a single string.

Looping Through Arguments

Use a for loop to process all arguments dynamically.

Example:

#!/bin/bash echo "Arguments passed:" for arg in "$@"; do echo "$arg" done

Execution:

bash script.sh apple banana cherry

Output:

Arguments passed: apple banana cherry

Checking Argument Count

You can ensure the script has the required number of arguments before proceeding.

Example:

#!/bin/bash if [ $# -lt 2 ]; then echo "Usage: $0 arg1 arg2" exit 1 fi echo "Arguments are: $1 and $2"

Execution:

bash script.sh Hello

Output:

Usage: script.sh arg1 arg2

Handling Flags and Options

Flags or options are common in command-line tools (e.g., -h, --help). These can be implemented in Bash using a while loop.

Example:

#!/bin/bash while [[ "$#" -gt 0 ]]; do case $1 in -h|--help) echo "Usage: $0 [options]" echo "-h, --help Show this help message" exit 0 ;; -n|--name) name="$2" shift ;; -a|--age) age="$2" shift ;; *) echo "Unknown option: $1" exit 1 ;; esac shift done echo "Name: $name" echo "Age: $age"

Execution:

bash script.sh -n John -a 30

Output:

Name: John Age: 30

Combining Arguments with Input Validation

Example: A Calculator Script

#!/bin/bash if [ $# -ne 3 ]; then echo "Usage: $0 num1 operator num2" exit 1 fi num1=$1 operator=$2 num2=$3 case $operator in +) echo "$num1 + $num2 = $((num1 + num2))" ;; -) echo "$num1 - $num2 = $((num1 - num2))" ;; \*) echo "$num1 * $num2 = $((num1 * num2))" ;; /) echo "$num1 / $num2 = $((num1 / num2))" ;; *) echo "Invalid operator. Use +, -, *, or /." ;; esac

Execution:

bash calculator.sh 5 + 3

Output:

5 + 3 = 8

Debugging Argument Handling

  • Print All Arguments:
    echo "All arguments: $@"
  • Exit on Error:
    set -e
  • Enable Debugging:
    set -x

Conclusion

Command line arguments make Bash scripts flexible and powerful. By using positional parameters, validation, and option handling, you can create scripts that adapt to a wide range of use cases.

Let me know if you'd like additional advanced examples!

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close