Size: 1436
Comment:
|
Size: 2351
Comment: Special Variables reference
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
<<TableOfContents>> |
|
Line 37: | Line 39: |
== Special Variables == {{{#!text_markdown * $0 - The filename of the current script. Do not use! * $n - The Nth argument passed to script was invoked or function was called. E.g. $1, etc. * $# - The number of argument passed to script or function. * $@ - All arguments passed to script or function. * $* - All arguments passed to script or function. * $? - The exit status of the last command executed. * $$ - The process ID of the current shell. For shell scripts, this is the process ID under which they are executing. * $! - The process number of the last background command. * $- - current options set for the shell * $_ - most recent parameter, or the absolute path of the command used to start the current shell See [Special Parameters from the Bash Reference Manual](https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html). }}} |
Contents
Interesting bash aliases and functions:
https://github.com/Bash-it/bash-it: Community bash "framework"
Google's shell script style guide. Also good is Bash 3 Boilerplate
Neat features
-v for testing set variables, while handling undefined ones
-v: True if the shell variable varname is set (has been assigned a value).
bash >=4.2 supports -v for testing for *set* variables while handling empty ones, also no specials needs when running under 'set -u':
also:
If you use bash, you should really use ]] instead of [ ]. It can do everything [ can (and then some, like ERE and glob matches), but is a shell keyword, which improves ergonomics quite a bit. See [ $x = 0 ] vs. [[ $x = 0 after unset x, for example.
Special Variables
- $0 - The filename of the current script. Do not use!
-
$n - The Nth argument passed to script was invoked or function was called. E.g. $1, etc.
-
$# - The number of argument passed to script or function.
- $@ - All arguments passed to script or function.
- $* - All arguments passed to script or function.
- $? - The exit status of the last command executed.
- $$ - The process ID of the current shell. For shell scripts, this is the process ID under which they are executing.
-
$! - The process number of the last background command.
-
$- - current options set for the shell
- $_ - most recent parameter, or the absolute path of the command used to start the current shell
See Special Parameters from the Bash Reference Manual.
Tools
Use shellcheck with timonwong.shellcheck plugin.
Use bash-language-server.