Shell Script Input Arguments in Linux
Shell Script Input Arguments
In this blog, the input arguments of shell script will be discussed. Shell script supports three types of input statements. They are
- Static Inputs
- Command Arguments (Positional Arguments)
- Dynamic Arguments (Runtime Arguments)
1. Static Inputs (Static Arguments)
- Process of assigning the values to the variables is called as static inputs
- This is done by using the equal operator named =
variable-name=initial-value
Example
i=99
Script Example
(is1.sh)
Source Code
echo "-----------------------"
echo -e "\t Static Inputs"
echo "-----------------------"
name="Sachin"
id=91
echo -e "Name\t: $name"
echo -e "Id\t\t: $id"
Note
- Here option -e allows the escape characters such as /t, /n, etc,...
Output
2. Command Arguments (Positional Arguments)
- Process of assigning the values to the script at the time of execution is called as command inputs
- This is done by using the special parameters like
- $0 indicates the filename or script itself
- $1 indicates the first argument
- $2 indicates the second argument
- ...
- $n indicates the nth argument.
- $# indicates the total number of arguments submitted which is the integer value.
Script Example
(is2.sh)
Source Code
echo "----------------------------"
echo -e "\t Command Inputs"
echo "----------------------------"
echo "First Argument: $1"
echo "Second Argument: $2"
echo "Total Arguments Passed: $#"
Output
3. Dynamic Inputs (Runtime Inputs)
- Process of assigning the values to the variables at the time of execution (at the runtime) is called as runtime arguments
- This is done by using the built-in command named read.
Script Example
(is3.sh)
Source Code
echo "-----------------------"
echo -e "\t Dyanmic Inputs"
echo "-----------------------"
echo "Enter your Name: "
read name
echo "Enter your Id: "
read id
echo -e "Name\t: $name"
echo -e "Id\t\t: $id"
Video Demo
For more interesting topics, follow our you tube channel
Comments
Post a Comment