开发者

Special variables in Unix shells? [closed]

开发者 https://www.devze.com 2023-01-06 16:41 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 3 years ago.

Improve this question 开发者_运维知识库

I need a good reference on unix shell special variables (csh, ksh and bash).

e.g. $1, $*, $#, etc.


Here: http://sillydog.org/unix/scrpt/scrpt2.2.2.php

$1 - $9 these variables are the positional parameters.

$0 the name of the command currently being executed.

$# the number of positional arguments given to this invocation of the shell.

$? the exit status of the last command executed is given as a decimal string. When a command completes successfully, it returns the exit status of 0 (zero), otherwise it returns a non-zero exit status.

$$ the process number of this shell - useful for including in filenames, to make them unique.

$! the process id of the last command run in the background.

$- the current options supplied to this invocation of the shell.

$* a string containing all the arguments to the shell, starting at $1.

$@ same as above, except when quoted.

More resources :

  • http://linuxshellaccount.blogspot.com/2008/04/shell-special-variables-in-bash.html
  • http://www.tutorialspoint.com/unix/unix-special-variables.htm


The best reference there is, is The POSIX Standard Shell Specification, especially the section on Special Parameters.


From the Linux Documentation Project (with additional $_ variable) :

$* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable.

$@ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word.

$# Expands to the number of positional parameters in decimal.

$? Expands to the exit status of the most recently executed foreground pipeline.

$- A hyphen expands to the current option flags as specified upon invocation, by the set built-in command, or those set by the shell itself (such as the -i).

$$ Expands to the process ID of the shell.

$! Expands to the process ID of the most recently executed background (asynchronous) command.

$0 Expands to the name of the shell or shell script.

$_ The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list. Subsequently, it expands to the last argument to the previous command, after expansion. It is also set to the full pathname of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.


Explanations and examples are in this Bash Special Parameters Explained with 4 Example Shell Scripts for: $*, $@, $#, $$, $!, $?, $-, $_.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号