Command Line Arguments in Shell Scripts
The command line arguments are important in shell scripts.
This help users to pass values directly to the script using command line.
Command Line Arguments in Linux Shell Scripting
Reading command line arguments
Lets try it with an example script: cmd-line-arg.sh
#!/bin/sh # Script that demos, command line args echo "Total number of command line argument are $#" echo "$0 is script name" echo "$1 is first argument" echo "$2 is second argument" echo "All of them are :- $* (or) $@"
Running the script: cmd-line-arg.sh
chmod 755 cmd-line-arg.sh
./cmd-line-arg.sh 1 2 3 4 last
Total number of command line argument are 5
./cmd-line-arg.sh is script name
1 is first argument
2 is second argument
All of them are :- 1 2 3 4 last (or) 1 2 3 4 last
Important:
$* (or) $@ can be used to print or read all the command line arguments.
$1 is the first argument, $2 the second, $3 the third and so on.
Related posts:
- Shell Scripting Introduction
- Shell Syntax
- Shell - Strings
- Shell - Variables
- Shell - User Intraction
- First Shell Script
- Shell - Arithmetics
- Shell - Case Statement
- Shell - Command Line Arguments
- Shell - For Loop
- Shell - While Loops
- Shell - IF Statements
- Shell - Input Output Redirections
- Shell - Pipes and Filters
- Shell - Process Management
Your IP address1 is: 172.18.0.2
Tutorials
Latest Updates
Follow us
- Tech-Tutorials twitter.com/techcuriosity