How to access command line arguments in a bash script
In Category Bash
The following example script illustrates how command line arguments passed to a bash shell script can be accessed. A special variable $0 contains name of the command and $# contains number of command line arguments passed to the script. Actual arguments can be accessed using the argument number prefixed with $ sign. i.e The first argument is accessed using $1 and $2 to access second argument so on and so forth.
[neo@techpulp ~]# cat cmdargs.sh #!/bin/bash echo Name of script: $0 echo First argument: $1 echo Second argument: $2 echo Number of arguments: $# echo All arguments: $@ [neo@techpulp ~]# [neo@techpulp ~]# sh cmdargs.sh arg1 arg2 Name of script: cmdargs.sh First argument: arg1 Second argument: arg2 Number of arguments: 2 All arguments: arg1 arg2 [neo@techpulp ~]#
Recent Comments