-
Notifications
You must be signed in to change notification settings - Fork 1
Your environment
A number of variables such as the name of your home directory, your username and default text editor are stored in something called environmental variables. To view these you use the command env.
[mtop@albiorix ~]$ env
PDSHROOT=/opt/pdsh
BIOROLL=/opt/bio
HOSTNAME=albiorix.bioenv.gu.se
TERM=xterm
SHELL=/bin/bash
ECLIPSE_HOME=/opt/eclipse
HISTSIZE=1000
HMMER_DB=/home/mtop/bio/hmmer/db
USER=mtop
PWD=/home/mtop
...
[mtop@albiorix ~]$Here we see, for example, that my username is mtop and the computer I'm loged into is albiorix.bioenv.gu.se (HOSTNAME). We can also retrieve values for specific variables like this:
[mtop@albiorix ~]$ echo $SHELL
/bin/bash
[mtop@albiorix ~]$SHELL is the name of the environmental variable, and $SHELL represents the value of that variable. Try for example to type echo SHELL and see what happens.
An alternative way of finding information about your environment is with this command:
[mtop@albiorix ~]$ env | grep "PYTHONPATH"
PYTHONPATH=/opt/app-root/src/.local/lib/python3.8/site-packages
[mtop@albiorix ~]$Now we know how to view our environmental variables and the next step is to add new ones. Let's say we want to store the current time and date in a variable so that we can use it later. We can call the variable NOW but first let's make sure it does not already exist:
[mtop@albiorix ~]$ echo $NOW
[mtop@albiorix ~]$Great. This variable name is free, as indicated by echo only returning an empty line. Next we'll store the current time and date in NOW:
[mtop@albiorix ~]$ export NOW=`date`
[mtop@albiorix ~]$ echo $NOW
Thu Oct 22 22:08:02 CEST 2015
[mtop@albiorix ~]$Make sure you find the character ` (which is different from ´ and ') on your keyboard. Here, date is enclosed by ` characters which indicates that the output from the program should be stored in the variable NOW. Compare this to just typing export NOW=date. What is the output of echo $NOW? Don't forget to reset the variable to the current time and date afterwards with the command:
export NOW=`date`
Let's wrap things up and put our new variable to use in a small (but rather silly) oneliner.
[mtop@albiorix ~]$ echo "The exercise started on $NOW and finished on `date`"
The exercise started on Thu Oct 22 22:21:54 CEST 2015 and finished on Thu Oct 22 22:27:04 CEST 2015
[mtop@albiorix ~]$