Click Back to learn the path of the Linux command line and shell script
The Bash shell provides a number of different ways to get data from users, including the following 3 methods:
- Command line arguments (data added in the back of fame)
- command-line options (a single letter that modifies the behavior of the command)
- Ability to read input directly from the keyboard
13.1-Command line parameters (direct manual processing of positional parameters)
The basic way to pass data to a shell script is to use command-line arguments to satisfy most of the simple requirements.
Command-line arguments run to add data to the command line when the script is run.
You have to know a few variables,
- $: The command itself, equivalent to the argv[0 in C + +]
- $: The first parameter.
- $ $4, $ ... : 2nd, 3, 4 parameters, and so on.
- The number of $# parameters, excluding the command itself
- [Email protected]: List of parameters themselves, not including the command itself
- $*: Same as [email protected], but "$*" and "[email protected]" (quoted) and different,
"$*" interprets all the parameters as a string,
"[Email protected]" is an array of parameters.
Note the points:
- If you enter more command-line arguments, each parameter must be separated by a space
- If you include spaces in the parameter values, you must use quotation marks (either single or multiple quotes)
- After the 9th variable, you must add curly braces around the variable number, such as ${10}
Example 1:
Example 2:
Use $ #检查运行脚本携带的参数个数 instead of the-N test to check command-line arguments
Example 3:
Example 4:
Self-study Linux shell13.1-command line parameters