Passing Arguments to the Script

LinuxLinuxBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

When writing scripts in Shell programming, you can pass arguments to the script when executing it. These arguments can be accessed within the script using special variables. This tutorial will guide you on how to pass arguments to a Shell script and how to handle and use them within the script.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-153895{{"`Passing Arguments to the Script`"}} linux/echo -.-> lab-153895{{"`Passing Arguments to the Script`"}} linux/cd -.-> lab-153895{{"`Passing Arguments to the Script`"}} linux/chmod -.-> lab-153895{{"`Passing Arguments to the Script`"}} shell/shebang -.-> lab-153895{{"`Passing Arguments to the Script`"}} shell/comments -.-> lab-153895{{"`Passing Arguments to the Script`"}} shell/quoting -.-> lab-153895{{"`Passing Arguments to the Script`"}} shell/variables_usage -.-> lab-153895{{"`Passing Arguments to the Script`"}} shell/for_loops -.-> lab-153895{{"`Passing Arguments to the Script`"}} shell/cond_expr -.-> lab-153895{{"`Passing Arguments to the Script`"}} shell/globbing_expansion -.-> lab-153895{{"`Passing Arguments to the Script`"}} end

Define the script and access the arguments

Create a new file and save it with ~/project/arguments.sh.

Inside the script, you can access the passed arguments using special variables. The $1 variable references the first argument, $2 references the second argument, and so on. The $0 variable holds the name of the script itself.

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

Execute the script with arguments

To execute the script, simply run the following command in your terminal, replacing the arguments with your own values:

cd ~/project
chmod +x arguments.sh
./arguments.sh argument1 argument2
Script name: ./arguments.sh
First argument: argument1
Second argument: argument2

Handle multiple arguments

You can handle multiple arguments by accessing the variables $1, $2, $3, and so on, depending on the number of arguments passed.

#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Third argument: $3"
$ ./arguments.sh argument1 argument2 argument3
Script name: ./arguments.sh
First argument: argument1
Second argument: argument2
Third argument: argument3

Use the $## variable

The $## variable holds the number of arguments passed to the script. You can use it to check the number of arguments and perform different actions based on that.

#!/bin/bash
if [ $## -eq 0 ]; then
  echo "No arguments passed."
elif [ $## -eq 1 ]; then
  echo "Only one argument passed."
else
  echo "Multiple arguments passed."
fi
$ ./arguments.sh
No arguments passed.
$ ./arguments.sh argument1
Only one argument passed.
$ ./arguments.sh argument1 argument2
Multiple arguments passed.

Use the $@ variable

The $@ variable holds a space-delimited string of all the arguments passed to the script. You can use it to loop through all the arguments and perform actions on each one.

#!/bin/bash
for arg in $@; do
  echo "Argument: $arg"
done
$ ./arguments.sh argument1 argument2 argument3
Argument: argument1
Argument: argument2
Argument: argument3

Summary

In this tutorial, you have learned how to pass arguments to a Shell script and how to handle and use them within the script. You now know how to access the arguments using the $1, $2, and $3 variables, and how to check the number of arguments using the $## variable. You have also learned how to loop through all the arguments using the $@ variable.

Other Linux Tutorials you may like