Builtin variables:
$BASH | The path to the Bash binary itself |
$BASH_ENV | An environmental variable pointing to a Bash startup file to be read when a script is invoked. |
$BASH_SUBSHELL | A variable indicating the subshell level. |
$BASHPID | Process ID of the current instance of Bash. This is not the same as the $$ variable, but it often gives the same result. |
$BASH_VERSION | The version of Bash installed on the system |
$CDPATH | A colon-separated list of search paths available to the cd command, similar in function to the $PATH variable for binaries. |
$DIRSTACK | The top value in the directory stack (affected by pushd and popd) |
$EDITOR | The default editor invoked by a script, usually vi or emacs |
$EUID | "effective" user ID number.Identification number of whatever identity the current user has assumed, perhaps by means of su. |
$FUNCNAME | Name of the current function |
$GLOBIGNORE | list of filename patterns to be excluded from matching in globbing. |
$GROUPS | Groups current user belongs to |
$HOME | Home directory of the user, usually /home/username |
$HOSTNAME | The hostname command assigns the system host name at bootup in an init script. |
$HOSTTYPE | host type |
$IFS | internal field separator |
$LC_CTYPE | This internal variable controls character interpretation in globbing and pattern matching. |
$LINENO | This variable is the line number of the shell script in which this variable appears. |
$MACHTYPE | machine type |
$OLDPWD | Old working directory ("OLD-Print-Working-Directory", previous directory you were in). |
$OSTYPE | operating system type |
$PATH | Path to binaries, usually /usr/bin/, /usr/X11R6/bin/, /usr/local/bin, etc. |
$PIPESTATUS | Array variable holding exit status(es) of last executed foreground pipe. |
$PPID | The $PPID of a process is the process ID (pid) of its parent process. |
$PROMPT_COMMAND | A variable holding a command to be executed just before the primary prompt, $PS1 is to be displayed. |
$PS1 | This is the main prompt, seen at the command-line |
$PS2 | The secondary prompt, seen when additional input is expected. It displays as ">". |
$PS3 | The tertiary prompt, displayed in a select loop |
$PS4 | The quartenary prompt, shown at the beginning of each line of output when invoking a script with the -x option. It displays as "+". |
$PWD | Working directory (directory you are in at the time) |
$REPLY | The default value when a variable is not supplied to read. |
$SECONDS | The number of seconds the script has been running. |
$SHELLOPTS | The list of enabled shell options, a readonly variable. |
$SHLVL | Shell level, how deeply Bash is nested.If, at the command-line, $SHLVL is 1, then in a script it will increment to 2. |
$TMOUT | If the $TMOUT environmental variable is set to a non-zero value time, then the shell prompt willtime out after $time seconds. This will cause a logout. |
$UID | User ID number. Current user's user identification number, as recorded in /etc/passwd |
Positional Parameters
$0, $1, $2, | Positional parameters, passed from command line to script, passed to a function, or set to a variable |
$# | Number of command-line arguments or positional parameters |
$* | All of the positional parameters, seen as a single word."$*" must be quoted. |
$@ | Same as $*, but each parameter is a quoted string, that is, the parameters are passed on intact, without interpretation or expansion."$@" should be quoted.The $@ and $* parameters differ only when between double quotes. |
$- | Flags passed to script (using set). |
$! | PID (process ID) of last job run in background |
$_ | Special variable set to final argument of previous command executed. |
$? | Exit status of a command, function, or the script itself |
$$ | Process ID (PID) of the script itself. [39] The $$ variable often finds use in scripts to construct "unique" temp file names |
Manipulating Strings
String Length | echo ${#string} echo `expr length $string` echo `expr "$string" : '.*'` |
Length of Matching Substring at Beginning of String | echo `expr match "$string" '$substring'` echo `expr "$string" : '$substring'` |
Index | echo `expr index $string $substring` |
Substring Extraction | ${string:position} ${string:position:length} |
Substring Removal | ${string#substring} Deletes shortest match of $substring from front of $string. ${string##substring} Deletes longest match of $substring from front of $string. ${string%substring} Deletes shortest match of $substring from back of $string. ${string%%substring} Deletes longest match of $substring from back of $string. |
Substring Replacement | ${string/substring/replacement} Replace first match of $substring with $replacement. ${string//substring/replacement} Replace all matches of $substring with $replacement. ${string/#substring/replacement} If $substring matches front end of $string, substitute $replacement for $substring. ${string/%substring/replacement} If $substring matches back end of $string, substitute $replacement for $substring.
|
Variable expansion / Substring replacement | ${var:pos} Variable var expanded, starting from offset pos. ${var:pos:len} Expansion to a max of len characters of variable var, from offset pos. ${var/Pattern/Replacement} First match of Pattern, within var replaced with Replacement ${var//Pattern/Replacement}All matches of Pattern, within var replaced withReplacement. |
Typing variables
declare/typeset options | -r | readonly |
|
| -i | Integer | declare -i number |
| -a | array | declare -a indices |
| -f | function(s) | declare -f function_name |
| -x | export | declare -x var3 |
|
|
|
|
generate random integer$RANDOM | $RANDOM is an internal Bash function (not a constant) that returns a pseudorandom integer in the range 0 - 32767. It should not be used to generate an encryption key. |
The Double-Parentheses Construct((...)) | the (( ... )) construct permits arithmetic expansion and evaluation. |