Seven Unexpected Bash Variables
- Transfer
- Tutorial
Continuing with a series of notes on lesser-known bash functions , I'll show you seven variables you might not know about.
1)
You may already be aware of how to manipulate the prompt prompt to display various useful information, but not everyone knows that you can run a shell command each time you display the prompt.
In fact, many complex prompt manipulators use this variable to execute commands to collect the information that is displayed at the prompt.
Try running this in a new shell and you will see what happens with the session:
2)
If you run
As soon as this variable is set, new records record time together with the command, so the output will look like this:
Formatting matches characters from man date.
3)
To save time on the command line, you can use this variable to change directories as easily as you invoke commands.
Like
If set
and then enter:
then you will always fall in
However, be careful, because if you do not specify a local (
Oops!
This is similar to the confusion I felt when I realized that the local folder was not included in the more familiar variable
My is set by the starting point:
4)
Have you ever wondered if input
This variable keeps track of how deeply you are embedded in the bash shell. If you create a new terminal, then it is set to 1:
Then, if you start another shell process, the number increases:
This can be very useful in scripts where you are not sure whether to exit or not, or to track where you are by nesting.
5)
Also, to analyze the current state and debugging, a variable is useful
This is most often used when debugging scripts. By inserting lines such as
6)
If, like me, you usually write code like this:
it may come as a surprise that you don’t have to worry about creating a variable at all:
It does the same.
7)
In order not to remain on production servers for too long for security reasons or accidentally run something dangerous in the wrong terminal, setting this variable acts as protection.
If nothing is entered within the set number of seconds, the shell exits.
That is, this is an alternative
1) PROMPT_COMMAND
You may already be aware of how to manipulate the prompt prompt to display various useful information, but not everyone knows that you can run a shell command each time you display the prompt.
In fact, many complex prompt manipulators use this variable to execute commands to collect the information that is displayed at the prompt.
Try running this in a new shell and you will see what happens with the session:
$ PROMPT_COMMAND='echo -n "writing the prompt at " && date'2) HISTTIMEFORMAT
If you run
historyin the console, you will get a list of commands previously executed under your account.$ HISTTIMEFORMAT='I ran this at: %d/%m/%y %T 'As soon as this variable is set, new records record time together with the command, so the output will look like this:
1871 I ran this at: 01/05/19 13:38:07 cat /etc/resolv.conf
1872 I ran this at: 01/05/19 13:38:19 curl bbc.co.uk
1873 I ran this at: 01/05/19 13:38:41 sudo vi /etc/resolv.conf
1874 I ran this at: 01/05/19 13:39:18 curl -vvv bbc.co.uk
1876 I ran this at: 01/05/19 13:39:25 sudo su -Formatting matches characters from man date.
3) CDPATH
To save time on the command line, you can use this variable to change directories as easily as you invoke commands.
Like
PATH, the variable CDPATHis a colon separated list of paths. When you run a command cdwith a relative path (that is, without a slash at the beginning), by default the shell looks for the corresponding names in your local folder. CDPATHwill search in the paths you gave for the directory where you want to go. If set
CDPATHin this way:$ CDPATH=/:/liband then enter:
$ cd /home
$ cd tmpthen you will always fall in
/tmpno matter where you are. However, be careful, because if you do not specify a local (
.) folder in the list , then you cannot create any other folder tmpand go to it, as usual:$ cd /home
$ mkdir tmp
$ cd tmp
$ pwd
/tmpOops!
This is similar to the confusion I felt when I realized that the local folder was not included in the more familiar variable
PATH... but you have to do it in the PATH variable because you can be tricked by running a fake command from some downloaded code. My is set by the starting point:
CDPATH=.:/space:/etc:/var/lib:/usr/share:/opt4) SHLVL
Have you ever wondered if input
exitwill take you from the current bash shell to another “parent” shell or just close the console window completely? This variable keeps track of how deeply you are embedded in the bash shell. If you create a new terminal, then it is set to 1:
$ echo $SHLVL
1Then, if you start another shell process, the number increases:
$ bash
$ echo $SHLVL
2This can be very useful in scripts where you are not sure whether to exit or not, or to track where you are by nesting.
5) LINENO
Also, to analyze the current state and debugging, a variable is useful
LINENOthat reports the number of commands executed in the session to date:$ bash
$ echo $LINENO
1
$ echo $LINENO
2This is most often used when debugging scripts. By inserting lines such as
echo DEBUG:$LINENO, you can quickly determine where in the script you are (or not).6) REPLY
If, like me, you usually write code like this:
$ read input
echo do something with $inputit may come as a surprise that you don’t have to worry about creating a variable at all:
$ read
echo do something with $REPLYIt does the same.
7) TMOUT
In order not to remain on production servers for too long for security reasons or accidentally run something dangerous in the wrong terminal, setting this variable acts as protection.
If nothing is entered within the set number of seconds, the shell exits.
That is, this is an alternative
sleep 1 && exit:$ TMOUT=1