Bash Scripting for Beginners: Complete Guide to Getting Started - Arguments (Part 16)
Table of Contents
Introduction
In this tutorial, you will learn how to effectively supply and utilize arguments in Bash scripts. Understanding how to work with arguments is crucial for creating flexible and dynamic scripts that can perform various tasks based on user input. This guide is based on the video "Bash Scripting for Beginners: Complete Guide to Getting Started - Arguments (Part 16)."
Step 1: Using a Simple Argument in Bash
To get started with arguments in Bash, follow these steps:
-
Create a Bash Script: Open your terminal and create a new script file, for example
my_script.sh
.touch my_script.sh chmod +x my_script.sh
-
Edit the Script: Open the file in your preferred text editor.
nano my_script.sh
-
Write the Script:
- Use the following code to access the first argument:
#!/bin/bash echo "The first argument is: $1"
-
Run the Script: Execute your script with an argument.
./my_script.sh Hello
- This will output:
The first argument is: Hello
.
- This will output:
Step 2: Using Multiple Arguments in Bash
Now, let's extend our script to handle multiple arguments.
-
Modify the Script: Update
my_script.sh
to include more arguments.#!/bin/bash echo "The first argument is: $1" echo "The second argument is: $2"
-
Run the Script: Execute the script with two arguments.
./my_script.sh Hello World
- Output will be:
The first argument is: Hello The second argument is: World
- Output will be:
Step 3: A Simpler Example of Using an Argument
For a more straightforward implementation, you can create a script that simply echoes back the argument provided.
-
Create a New Script: Name it
echo_arg.sh
.touch echo_arg.sh chmod +x echo_arg.sh
-
Write the Script:
#!/bin/bash echo "You entered: $1"
-
Run the Script:
./echo_arg.sh Test
- This will display:
You entered: Test
.
- This will display:
Step 4: Counting Items in a Directory
You can also create a script that counts the number of items in a specified directory.
-
Create the Script: Name it
count_items.sh
.touch count_items.sh chmod +x count_items.sh
-
Write the Script:
#!/bin/bash if [ -d "$1" ]; then count=$(ls -1 "$1" | wc -l) echo "There are $count items in the directory $1" else echo "Directory $1 does not exist." fi
-
Run the Script:
./count_items.sh /path/to/directory
- This will display the number of items if the directory exists.
Step 5: Ensuring Arguments are Provided
It’s important to check if the required arguments are provided to avoid errors.
-
Modify the Count Script: Add a check at the beginning of your
count_items.sh
.#!/bin/bash if [ -z "$1" ]; then echo "Usage: $0 <directory>" exit 1 fi
-
Run the Script Without Arguments:
./count_items.sh
- The output will be:
Usage: ./count_items.sh <directory>
.
- The output will be:
Conclusion
You have now learned how to supply and use arguments in Bash scripts. By creating scripts that handle single and multiple arguments, echoing user input, counting directory contents, and ensuring arguments are provided, you can enhance your scripting capabilities. As a next step, consider exploring more complex scripting scenarios or looking into other Bash scripting tutorials to expand your skills further.