Friday 11 August 2017

Bash Parameters

Cited from the book "Pro Bash Programming"

Chapter 2
To access positional parameters greater than 9, the number must be enclosed in braces: ${15}.

The function shift N moves the positional parameters by N positions, if you ran shift (the default value of N is 1), then $0 would be discarded, $1 would become $0, $2 would become $1, and so on: they would all be shifted by 1 position.

The two special parameters, $* and $@, expand to the value of all the positional parameters combined. $# 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 exit code of the last-executed command, and $_ is set to the last argument to that command. $! contains the PID of the last command executed in the background, and $- is set to the option flags currently in effect.

%b is like %s except that escape sequences in the arguments are translated.

printf "%s\n" "Hello\nworld" "12\tword", vs.
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.

bash added a -v option to store the output in a variable instead of printing it to the standard output. 
printf -v num4 "%04d" 4

Input/Output streams are referred to by numbers, called file descriptors (FDs). These are 0, 1, and 2, respectively. The stream names are also often contracted to stdin, stdout, and stderr.

Redirection is performed before any command on the line is executed. If you redirect to the same file you are reading from, that file will be truncated, and the command will have nothing to read.

Redirecting standard output does not redirect standard error. Error messages will still be displayed on your monitor.

Instead of sending output to a file, it can be redirected to another I/O stream by using >&N where N is the number of the file descriptor.

For the 'read' command only the -r option is recognized by the POSIX standard. It tells the shell to interpret escape sequences literally.

No comments:

Post a Comment