Linux Bash Default Environment Variables
While working with Bash environment we generally need some variable data to set and get. Bash shell provides some default values about current user, hostname etc. as environment variable. In this tutorial we will look most popular of them.
Get The User Running Script
While running bash interactively or non-interactively getting current user name is done with $USER special variable.
$ echo $USER
Host name is the name of the computer that is currently used. In enterprise environment there are a lot of servers and hosts. It can be distinguishing host name is important for scriptwriters. To get host name where the script is running
$ echo $HOSTNAME
Get Number of Seconds After Shell Started
To get time between now and shell start time use this special variable. If the shell you use is all ready open for a long time you will get a big number of course.
$ echo $SECONDS
Generate Random Numbers
To generate random numbers use the following command. Be aware that this random number generation method may not be photographically secure. To generate most secure random numbers use Openssl library with extra hardware. But Openssl random number generation is enough for a lot of situation.
$ echo $RANDOM
Get Current Line Number of The Bash Script
Line number can be used in bash scripts or interactive shell and for both of them gives line number from scratch.
$ echo $LINENO
Get Currently Running Script Path
Some scripts run a lot of complex operations. through the operations the path of the currently running script is important. To get running bash script current working directory use BASH_SOURCE variable like below.
$ echo $BASH_SOURCE