special variables in the shell

When defining a variable, the variable name does not add a dollar sign ($, required for variables in the PHP language), such as:

your_name="runoob.com"

Note that there can be no spaces between the variable name and the equals sign, which may be different from all programming languages ​​you are familiar with. At the same time, the naming of variable names must follow the following rules:

  • The naming can only use English letters, numbers and underscores, and the first character cannot start with a number.
  • There can be no spaces in between, you can use an underscore (_).
  • Punctuation marks cannot be used.

  • Cannot use keywords in bash (reserved keywords can be viewed with the help command)

Variable names can only contain numbers, letters and underscores, because some variables containing other characters have special meanings, such variables are called special variables.

$0 Get the file name including the path of the currently executing script

    dirname $0 only takes the path of the currently executing script 

     dirname outputs the name with the trailing "/" character part removed; if the name does not contain "/", "." (representing the current directory) is displayed.

    basename $0 only takes the filename of the currently executing script

     basename Prints "name" with the leading directory part removed. Trailing "suffix" is also stripped if specified.

$#  Get the total number of execution command line (script) parameters

$@ executes all parameters of the program

$*  Get all parameters of the current shell (note the difference from $@)

    Difference between $* and $@

    Both $* and $@ represent all parameters passed to a function or script. When not enclosed in double quotes "", all parameters are output in the form of $1 $2 .. $n.

    But when they are enclosed in double quotes "", "$*" will output all parameters as a whole, in the form of "$1 $2 $3 ... $n"; "$@" will separate each parameter , output all arguments in the form "$1" "$2" ...."$n".


$! PID of the last executed command

$$ Get the PID of the current shell

$_ The last parameter of the command or script executed before this

$? The exit status of the previous command, that is, the return result after the execution of the previous command.

    Return value: 0 means successful operation 2 Rejected by the whole county 1-125 Operation failed, script command, system command or parameter transmission error 126 Command found but cannot be executed 127 Command to be run not found 128 The command was forcibly terminated by the system

$n Arguments passed to the script or function. n is a number indicating the number of parameters. Such as: $1 first parameter, $2 second parameter






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325395875&siteId=291194637