Linux Bash Default Environment Variables

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-default-environment-variables/
PINTEREST
PINTEREST
INSTAGRAM

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.

Get Hostname

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

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.

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.

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.

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.

LEARN MORE  How To List Currently Logged In Users In Linux?

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-default-environment-variables/
PINTEREST
PINTEREST
INSTAGRAM

Leave a Reply

Your email address will not be published.