Bash Script Arguments

Bash Script Arguments

Reading the command arguments in Bash script

Command line arguments are the arguments passed to a program or script when it is executed. They provide a way to customize the behavior of the program or script without having to modify its code.

We call a shell script that can handle arguments by passing the arguments after the script name when executing it:

./script.sh arg1 arg2 arg3

Here script.sh is the script name and arg1, arg2 and arg3 are the command line arguments passed to the script.

Inside the script, the arguments can be accessed using:

  • $1 for the first argument

  • $2 for the second argument

  • and so on

$0 refers to the script name itself.

All arguments can be accessed using the special variable $@.

Command line arguments are useful for:

  • Passing different input data to a script

  • Passing options to customize the behavior of a script

  • Passing filenames and other parameters

So in summary, command line arguments allow passing information to shell scripts when executing them, giving them more flexibility and reusability.


Script Example

Here is an example of accessing command line arguments passed to a Bash script:

#!/bin/bash

echo "First argument: $1"
echo "Second argument: $2"
echo "Third argument: $3"

# Access all arguments 
for arg in "$@"; do
    echo "$arg"
done

You can access command line arguments in a Bash script using $1 for the first argument, $2 for the second, and so on.

For example, if you run the script like this:

./script.sh arg1 arg2 arg3

The output will be:

First argument: arg1  
Second argument: arg2
Third argument: arg3
arg1
arg2  
arg3

The special variable "$@" represents all arguments, so the for loop iterates through and echoes each argument.


Some Use-Cases

Here are some significant use cases for scripts that receive arguments:

  1. Passing input data - A script can be designed to accept input data as arguments. This allows the same script to be reused for different input data.

  2. Passing options - Arguments can be used to pass options to a script to customize its behavior. For example, to run a script in verbose mode, debug mode, etc.

  3. Passing filenames - Arguments can be used to pass filenames to a script which then processes those files.

  4. Parallel script execution - A script can be designed to accept a range of data as arguments. This allows executing multiple instances of the script in parallel, each processing a part of the data range. This can significantly improve performance.

For example:

script.sh 1 10  # Processes data from 1 to 10
script.sh 11 20 # Processes data from 11 to 20  
script.sh 21 30 # Processes data from 21 to 30
  1. Passing environmental variables - Arguments can be used to pass environmental variables to a script which then uses those variables.

So in summary, arguments allow scripts to:

  • Accept flexible input data

  • Be customized based on options

  • Operate on multiple files

  • Execute in parallel by dividing up data ranges

  • Access environmental variables

All of this makes scripts more flexible, reusable and performant. Hope this helps! Let me know if you have any other questions.


Disclaimer: This article was written with the assistance of an AI chatbot. The content should be considered a rough draft that requires human review. Please comment below if you find this article useful and correct or you think it should be modified. If yes, make a suggestion of improvement.