Environment is shell running session and session related info. environment is important part of the Linux bash. We can set variables at the current environment and use them. Bash also provides some default environment variables too.
Create Variables While Login
You may need to create variable at login time of the user. For example to use login date at shell scripts it is very convenient or looking data from other sources and setting for session is an other reason to create variables while login.There are different locations to make this possible.
- /etc/profile is the start point for shell creation. There is all ready a lot of script in this file. You can add simple at the end of file. This file needs user login to execute.
- /etc/profile.d/ is similar with profile file but serves more autonomous place to make changes. profile.d is directory that contains script files those are executed at run time. If you will need complex and more than few line script you can create a script file here. This file needs user login to execute
- ~/.bashrc is different from other because of the code executed without a login which means if there is no user that login we should use this file.To make complex script create another file and run it from bash.rc
Variables for bash can be set for different places. There are 4 types of variables
Like normal variables environment variables are case sensitive too. Exporting variables are declared with export keyword. Export keyword provides that variable will exist other shells too.
$ export test="This is test"
$ env | grep test
test=This is test
If you open new terminal you can see the defined variable but if you close existing terminal and look test variable in other terminal you can not find it because environment variables are only used by child terminals. Child terminal is a terminal that is created by existing terminal.
To unset environment variable use -n parameter with export like this. This will make environment variable normal variable.
env |grep test
export -n test
env |grep test
Create Shell Variables
There is default bash variables that helps about current environment. For example to find current bash binary path BASH variable is used.
$ echo $BASH
To find currently executed bash command
$ echo $BASH_COMMAND
Get Bash Version
Sometimes you will get a script from web or there is a problem with an existing script in the new host. There may be some compatibility issues. To get detailed info about compatibility bash version is important. And here is how to get it.
$ echo $BASH_VERSINFO
Find enviroment variables which are set when bash starts executing.
Getting The User Running Script
While running bash interactively or non-interactively getting current user name is done with $USER special variable. This will be helpful if the script needs root privileges and checking the current user.
$ echo $USER
Getting Host name
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
Getting 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
Generating 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
Getting 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