User Tools

Site Tools


bash

Variables

$0 My filename
$n positional parameter n
$n - $m positional paramters n thru m
${n} positional parameter n
“${*}” all positional parameters (single word)
“$@” all positional parameters (separate)
$# number of positional parameters
${#{*}} number of positional parameters
${#@} number of positional parameters
$? value returned by last command
$$ PID of current shell
$! process number of last background cmd
$_ Last argument of previous cmd

Miscellany

-x print commands as executed
$( ) in-line command substitution (new style)
` ` in-line command substitution (old style)
$((...)) in-line arithmetic evaluation

Prompts

\h short hostname
\H full hostname
\u username
\w current working directory
\W basename of current working directory
\$ # if the effective UID is 0, otherwise $

Variable Substitution

$name shell variable name
${name} delimit shell variable name
${name-word} variable nameif set, else word
${name=word} as above but also set name to word
${name?word} use name if set, otherwise print word and exit
${name+word} use word if name is set, otherwise use nothing
${namen} element n in array name
${#name} length of shell variable name
${#name*} number of elements in array name
${#name@} number of elements in array name
${name#pat} remove shortest leading substring of name that matches pat
${name##pat} remove longest leading substring of name that matches pat
${name%pat} remove shortest trailing substring of name that matches pat
${name%%pat} remove longest trailing substring of name that matches pat
${name:start} length characters of name starting at start(counting from0); use rest of value if no length. Negative start counts from the end
${name:start:length}
${name/pattern/string} value of name with first match of pattern replaced with string
${name/pattern} value of name with first match of pattern deleted
${name/#pattern/string} value of name with match of pattern replaced with string;match must occur at beginning
${name/%pattern/string} value of name with match of pattern replaced with string;match occurs at end
bash.txt · Last modified: 2017/11/10 02:15 by mark