How to Pass Command Line Arguments in Shell Scripting
Bash Command Line Arguments Explained
Introduction
Hello Folks 🙂 In this article, we will learn different ways of passing command-line arguments to Bash scripts.
Bash scripts accept arguments sequentially and can also parse them as options.
Positional Command Line Arguments
Bash input arguments are accessed using positions like $1, $2 ... $n.
$@ → Expands into separate parameters.
$* → Single string.
$# → Total arguments.
$0 denotes the script name.
Example - Command Line Script
#!/bin/bash echo "Script name: $0" echo "Input Arg 1: $1" echo "Input Arg 2: $2" echo "Input Arg 3: $3" echo "-----------" echo "All Input Args: $*" echo "All Input Args: $@" echo "Input Arg count: $#"
Run:
$ bash demo.sh 1 2 3
Conclusion
You learned how Bash handles command-line arguments and how to use them effectively.
No comments:
Post a Comment