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.
[email protected]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.

Please help us and share this article

About the author

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python!