Passing Parameters
The most basic method of passing data to your shell script is to use command line parameters. Command line parameters allow you to add data values to the command line when you execute the script:
$ ./addem 10 30
This example passes two command line parameters (10
and 30
) to the script addem
. The script handles the command line parameters using special variables. The following sections describe how to use command line parameters in your bash shell scripts.
Reading parameters
The bash shell assigns special variables, called positional parameters, to all of the command line parameters entered. This includes the name of the script the shell is executing. The positional parameter variables are standard numbers, with $0
being the script's name, $1
being the first parameter, $2
being the second parameter, and so on, up to $9
for the ninth parameter.
Here's a simple example of using one command line parameter in a shell script:
$ cat test1.sh
#!/bin/bash
# using...