Parse Command Line Arguments in Bash on Linux

Command-line arguments are parameters passed to bash scripts that allow users to customize script behavior without modifying the script itself. These arguments can be processed sequentially as positional parameters or parsed as options using built-in utilities like getopts and external tools like getopt.

Note ? Linux commands are case-sensitive.

Basic Command Line Arguments

Bash automatically stores command-line arguments in special variables:

  • $0 ? Script name

  • $1, $2, $3... ? Positional arguments

  • $# ? Number of arguments

  • $@ ? All arguments as separate strings

  • $* ? All arguments as a single string

#!/bin/bash
echo "Script name: $0"
echo "First argument: $1" 
echo "Second argument: $2"
echo "Total arguments: $#"

Using getopts for Option Parsing

The getopts built-in provides a structured way to parse single-character options. Its syntax is:

getopts optstring variable [arguments]
  • Options expecting arguments are followed by a colon (:) in the optstring

  • When an option has an argument, getopts stores it in the OPTARG variable

  • Invalid options are handled by placing ? in the variable

Example with getopts

#!/bin/bash
while getopts "f:v" opt; do
  case $opt in
    f) filename="$OPTARG" ;;
    v) verbose=true ;;
    \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;;
  esac
done
echo "File: $filename, Verbose: $verbose"

Using getopt for Advanced Parsing

The external getopt utility handles both short (-h) and long (--help) options, providing more flexibility than getopts.

Short and Long Options

  • Short options ? Single character with one hyphen (e.g., -h, -v)

  • Long options ? Full words with two hyphens (e.g., --help, --verbose)

Complete getopt Example

#!/bin/bash

SHORT=f:,v,h
LONG=file:,verbose,help
OPTS=$(getopt -a -n script --options $SHORT --longoptions $LONG -- "$@")

eval set -- "$OPTS"

while true; do
  case "$1" in
    -f | --file )
      filename="$2"
      shift 2
      ;;
    -v | --verbose )
      verbose=true
      shift
      ;;
    -h | --help )
      echo "Usage: $0 [OPTIONS]"
      echo "  -f, --file FILE    Specify input file"
      echo "  -v, --verbose      Enable verbose output" 
      echo "  -h, --help         Show this help message"
      exit 0
      ;;
    -- )
      shift
      break
      ;;
    * )
      echo "Unexpected option: $1"
      exit 1
      ;;
  esac
done

echo "Processing file: ${filename:-default.txt}"
[[ "$verbose" == "true" ]] && echo "Verbose mode enabled"

Usage Example

$ bash script.sh --file data.txt --verbose
Processing file: data.txt
Verbose mode enabled

$ bash script.sh -f input.csv -v
Processing file: input.csv  
Verbose mode enabled

Key Differences

Feature getopts getopt
Type Built-in shell command External utility
Long options No Yes
Portability High (POSIX) Lower (GNU-specific)
Complexity Simple More complex

Conclusion

Command-line argument parsing in bash can be handled through positional parameters for simple cases, getopts for basic option parsing, or getopt for advanced scenarios requiring long options. Choose the method based on your script's complexity and portability requirements.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements