A Command-line Arguments are passed after the name of a program in command-line operating systems like DOS or Linux and are passed into the program from the operating system. Shell scripts also accept command line arguments similar to nix commands. Command line arguments are useful for passing input to script at runtime which has its own advantage. This article will help you to pass command line arguments in a shell script.
To pass a command line argument we can simply write them after script name separated with space. All command line parameters can be access by their position number using $. A sample example of passing command line argument to shell script.
sh myScript.sh 10 rahul tecadmin.net sh: Linux shell myScript.sh: Linux shell script 10: First command line parameter accessible by $1 rahul: Second command line parameter accessible by $2 tecadmin.net: Third command line parameter accessible by $3
Access Command Line Argument with Position Number
So as above example command line parameters are accessible at $1, $2, $3…. $9, $10…. $100 etc. Maximum length of command line parameters are not defined by shell but it’s defined by operating system and measured in Kilobytes.
$*– Store all command line arguments [email protected]– Store all command line arguments $#– Store count of command line arguments $0– Store name of script itself $1– Store first command line argument $2– Store second command line argument $3– Store third command line argument .. $9– Store 9’th command line argument $10– Store 10’th command line argument … … $99– Store 99’th command line argument
Create a shell script to Print all Argument with script name and total number of arguments passed. create script file myScript.sh suing following content.
# vim myScript.sh
#!/bin/bash echo Script Name: "$0" echo Total Number of Argument Passed: "$#" echo Arguments List - echo 1. $1 echo 2. $2 echo 3. $3 echo All Arguments are: "$*"
Execute the script
sh myScript.sh10 rahul tecadmin.net Script Name: myScrit.sh Total Number of Argument Passed: 3 Arguments List - 1. 10 2. rahul 3. tecadmin.net All Arguments are: 10 rahul tecadmin.net
Create loop through all arguments in shell script. To do this create a shell script file myScript2.sh suing following content.
# vim myScript2.sh
#!/bin/bash for i in "[email protected]" do echo Argument: $i done
Execute the script
./myScript2.sh10 rahul tecadmin.net Argument: 10 Argument: rahul Argument: tecadmin.net
Access Command Line Argument by Shifting Position
We can also access all command line arguments by shifting their position in a shell script. Like access your first command line argument with $1. Now shift arguments with 1. It means the second argument is now at first position, similarity third is at second and so on.
Create a shell script myScript3.sh using below content and execute with argument. Now watch how parameters are shifted using “shift <number>” command in a shell script.
#!/bin/bash echo First Argument is: $1 echo " >> Shifting argument position by 1" shift 1 echo Now first Argument is: $1 echo " >> Now Shifting position with 2" shift 2 echo Now first Argument is: $1 echo " >> Now shifting position with 4" shift 4 echo Now first Argument is: $1
Execute the script and closely observe the output of $1 in script.
[[email protected] ~]# sh
myScrit3.sha friend in need is a friend indeed First Argument is: a>> Shifting argument position by 1 Now first Argument is: friend>> Now Shifting position with 2 Now first Argument is: need>> Now shifting position with 4 Now first Argument is: indeed
I hope this will help you for understanding command line arguments in a shell script. For any suggestion please add your comments.