useful bash expressions

Use man bash to check the description of the following experssions:
$ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell.
# Expands to the number of positional parameters in decimal
? Expands to the status of the most recently executed foreground pipeline
$# the number of parameters with which the script has been called
$? reads the exit status of the last command executed, $? == 0 if all commands exit successfully.After a function returns, $? gives the exit status of the last command executed in the function.
&& Logical AND
lets you do something based on whether the previous command completed successfull.More precisely, && will evaluate the second expression iff the first expression returns 0
exit command terminates a script, just as in a C program. It can also return a value, which is available to the script’s parent process.
exit 0 Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions.

&> Bash allows both the standard output (file descriptor 1) and the standard error output
(file descriptor 2) to be redirected to the file whose name is the expansion of word
with this construct.
There are two formats for redirecting standard output and standard error:
&>word
and
>&word
Of the two forms, the first is preferred.
&> /dev/null send standard output to blackhole

&> /dev/null send standard output to blackhole
>/dev/null 2>&1
There are always three default files open. stdin (0) stdout (1) stderr (2)
2>&1 is asking to direct all the STDERR as STDOUT, (ie. to treat all the error messages generated from the script as its standard output). Now we already have > /dev/null at the end of the script which means all the standard output (STDOUT) will be written to /dev/null. Since STDERR is now going to STDOUT (because of 2>&1) both STDERR and STDOUT ends up in the blackhole /dev/null. In other words, the script is silenced.

$0 The filename of the current script.
$n These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on).
$* All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
$? The exit status of the last command executed.
$$ The process number 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.

https://www.tutorialspoint.com/unix/unix-special-variables.htm

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s