How to access command line arguments in a bash script
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 ~]#


about 1 month ago
Very nice article, this hleped me greatly in writing a test script for a network emulator (many asynchronous processes). pidof didn’t work for me the way I needed to use it, and the line you provided was exactly what I needed. Thanks a lot!