Bash Variables

Just like other languages, Bash has variables. One peculiar thing about variables in Bash is that you access them with the dollar sign, but you set them without it. To clarify, here’s an example:

myvar="Hello world"
echo $myvar

Bash has a number of reserved variables, that are automatically set for you. The following table is a non-exhaustive list of some of the more useful ones:

$0Name of the current script.
$1 … $9First 9 arguments to the script.
$#Number of arguments passed to the script.
$@All the arguments supplied to the script.
$USERThe username of the user running the script.
$HOSTNAMEThe hostname of the machine the script is running on.
$SECONDSThe number of seconds since the script was started.
$RANDOMReturns a different random number each time is it referred to.
$LINENOReturns the current line number in the Bash script.