Taking Command Line Arguments in Bash
source link: https://www.devdungeon.com/content/taking-command-line-arguments-bash
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Taking Command Line Arguments in Bash
Submitted by NanoDano on Tue, 03/20/2018 - 01:54
Overview
Bash scripts are commonly used to perform a variety of tasks. These examples describe various ways you can work with command line arguments.
Pass arguments through to another program
Bash scripts are often used as wrappers to launch another application. A common task is to pass the command line arguments from the script to the program being loaded. Here is an example of passing all the arguments provided as-is.
#!/bin/bash
# print_args.sh
echo "You provided the arguments:" "$@"
# You could pass all arguments to another program like this
# myProgram "$@"
Ensure the script has the executable permission, and run it with a few arguments passed.
chmod +x print_args.sh
./print_args.sh 1 2 3
Get the number of arguments passed
The number of arguments passed is stored in the $# variable.
#!/bin/bash
echo "You provided $# arguments"
Accessing a specific argument by index
You can access a specific argument by its index like this:
#!/bin/bash
echo "Arg 0: $0"
echo "Arg 1: $1"
echo "Arg 2: $2"
Argument 0 is the name of the script being invoked itself.
Iterating through each argument
This example shows how to iterate through each argument one-by-one, and print out the value.
#!/bin/bash
for arg in "$@"
do
echo "$arg"
done
Check arguments for specific value
If you wanted to iterate through each argument and check if one equals a specific value, you can do that with the following:
#!/bin/bash
for arg in "$@"
do
if [ "$arg" == "--help" ] || [ "$arg" == "-h" ]
then
echo "Help argument detected."
fi
done
Then try running that while passing it a -h or --help flag.
Conclusion
Did you notice a trend in the variables? All of the command line argument variables started with a dollar sign $.
# All arguments
$@
# Number of arguments
$#
# Specific arguments
$0
$1
$2
With this knowledge, you should be able to work with the command line arguments provided to a bash script.
Advertisement
Recommend
-
9
Command-line arguments and flags yourbasic.org/golang The
-
3
Introduction When processing command line arguments in Python, you have a few options available: sys.argv - a raw list of arguments passed to the Python interpreter argparse - Python's...
-
10
Overview Introduction Taking command line arguments is one of the first things you should learn how to do with a new language. In this tutorial we'll walk through a simple Java program that takes command line arguments...
-
10
Using Command Line Arguments in Node.js Submitted by NanoDano on Sat, 08/01/2015 - 19:56...
-
9
Command Line Arguments in PHP Submitted by NanoDano on Mon, 07/27/2015 - 19:31 In PHP, all command line argumen...
-
1
Capturing Command Line Arguments in an Apollo Application Sunday, May 13, 2007 Here is a simple example that shows how to capture command line arguments passed to a Flex based Apollo application. &l...
-
5
Introduction Command-line arguments are a way to provide additional input for commands. You can use command-line arguments to add flexibility and customization to your Node.js scripts. In this article, you will learn about a...
-
4
Python Argparse: Parser for command-line options, arguments and sub-commands Knoldus Blog Audio...
-
7
Edge Command Line Arguments Microsoft Edge offers broad variety of configuration options via Group Policy (for Enterprises), the edge://settings page, the edge://flags page (mostly experim...
-
3
How to Pass Command-Line Arguments in Node.js (2022)In this article, I want to show how to pass command-line arguments in your Node.js application.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK