The Special parameters are: *@#0$?_!-
- $* expand to the value of all positional parameters combined
- $@ same as the above
- $# expands to the number of positional parameters.
- $0 contains the path to the currently running script or to the shell itself if no script is being executed.
- $$ contains the process identification number (PID) of the current process.
- $_ is set to the last argument to that command.
- $? is set to the exit code of the last-executed command.
- $! contains the PID of the last command executed in the background
- $- is set to the option flags currently in effect
A variable is a parameter denoted by a name; a name is a word containing only letters, numbers, or underscores and beginning with a letter or an underscore.
name=VALUE
NOTE: Bash is very particular about spacing: note that there are no spaces before the = and none after. If you have spaces, the command would not work.
printf
is derived from the C programming language function of the same name;
printf FORMAT ARG ...
Escape sequences are single letters preceded by a backslash:
\a::Alert
\b::Backspace
\e::Escape character
\f::Form feed
\n::Newline
\r::Carriage return
\t::Horizontal tab
\v::Vertical tab
\\::backslash
\nnn::A character specified by one to three octal digits
\xHH::A character specified by one or two hexadecimal digits
The backslashes must be protected from the shell by quotes or another backslash:
printf "Q\t\141\n\x42\n"
The format specifiers are letters preceded by a percent sign. Optional modifiers may be placed between the two characters. The specifiers are replaced by thecorresponding argument. When there are more arguments than specifiers, the format string is reused until all the arguments have been consumed. The most commonly used specifiers are %s, %d, %f, and %x.
The %s
specifier prints the literal characters in the argument:
$ printf "%s\n" Print arguments on "separate lines"
%b
is like %s except that escape sequences in the arguments are translated:
$ printf "%b\n" "Hello\nworld" "12\tword"
Integers are printed with %d. The integer may be specified as a decimal, octal (using a leading 0), or hexadecimal (preceding the hex number with 0x) number. If the number is not a valid integer, printf prints an error message:
$ printf "%d\n" 23 45 56.78 0xff 011