Command Line Argument
Command Line Argument
Execution:
bash script.sh Hello World
Output:
Script Name: script.sh
First Argument: Hello
Second Argument: World
Number of Arguments: 2
All Arguments: Hello World
Example: Sum of Two Numbers
#!/bin/bash
sum=$(( $1 + $2 ))
echo "Sum of $1 and $2 is: $sum"
Execution:
bash add.sh 10 20
Output:
Sum of 10 and 20 is: 30
Execution:
bash check.sh
Output:
No arguments provided!
Key Takeaways
Command-line arguments help pass dynamic inputs.
$0 represents the script name.
$1, $2, ... store individual arguments.
$# gives the number of arguments.
$@ holds all arguments as a list.