Note: The following content applies to the [*.sh] file
Variables given by the system
In Shell programming, system-given variables (also called predefined variables) are preset by the system and are used to provide information about the environment and status of the script running. These variables usually do not require user definition or modification and can be used directly in scripts.
variable name | effect |
$0 | The name of the current script |
$n | Parameters passed to the script or function, n represents the number of parameters |
$# | The number of arguments passed to the script or function |
$* | All parameters passed to the script or function |
$@ | All parameters passed to the script or function |
$$ | PID of the current shell script process |
$? | Function return value, or the exit status of the previous command |
$BASH | The path to the BASH binary file |
$BASH_ENV | BASH startup file |
$BASH_VERSINFO[n] | BASH version information, has six elements |
$BASH_VERSION | BASH version number |
$EDITOR | The default editor called by the script |
$EUID | Current valid user ID |
$FUNCNAME | current function name |
$GROUPS | The group to which the current user belongs |
$HOME | Current user's home directory |
$HOSTTYPE | Host type |
$ linen | Current line number |
$OSTYPE | Operating system type |
$PATH | PATH path |
$PPID | The parent process ID of the current shell process |
$PWD | current working directory |
$SECONDS | Current script running seconds |
$TMOUT | When it is not 0, the shell will exit after the specified seconds. |
$UID | Current user ID |
Instructions:
Edit the script file.
vi test.sh
Test content:
echo "文件名:$0"
echo "第一个参数:$1"
echo "第三个参数:$3"
echo "共计传递了:$#个参数"
echo "传递的所有参数:$*"
echo "传递给函数的参数:$@"
echo "当前脚本的进程ID:$$"
echo "二进制文件路径:$BASH"
echo "BASH的启动文件:$BASH_ENV"
echo "BASH的版本号6个元素:$BASH_VERSINFO"
echo "BASH的版本号:$BASH_VERSION"
echo "脚本使用的默认编辑器:$EDITOR"
echo "当前的用户ID(有效):$EUID"
echo "当前的用户ID:$UID"
echo "用户HOME目录:$HOME"
echo "主机类型:$HOSTTYPE"
echo "操作系统类型:$OSTYPE"
echo "PATH路径:$PATH"
echo "当前工作目录:$PWD"
echo "当前脚本运行秒数:$SECONDS"
Test Results:
Note here that the following BASH version number has 6 elements. It displays 5, which is 0, 1, 2, 3, 4, and 5, a total of six elements.
Many times we need to make certain system judgments and permission judgments, so we need these default contents, which are very convenient to use.