Seven Unexpected Bash Variables

Continuing the series of notes about lesser-known features of bash, I will show you seven variables you might not know about.

1) PROMPT_COMMAND

You may already be aware of how to manipulate the prompt to show various useful information, but not everyone knows that every time the prompt is displayed, a shell command can be executed.

In fact, many complex prompt manipulators use this variable to run commands to gather information displayed in the prompt.

Try running this in a new shell and see what happens to the session:

$ PROMPT_COMMAND='echo -n "writing the prompt at " && date'

2) HISTTIMEFORMAT

If you run history In the console, you will get a list of commands previously executed under your account.

$ HISTTIMEFORMAT='I ran this at: %d/%m/%y %T '

Once this variable is set, new entries log the time along 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 -

The formatting corresponds to the symbols from man date.

3) CDPATH

To save time in the command line, you can use this variable to change directories as easily as you invoke commands.

As well as PATH, the variable CDPATH is a list of paths separated by colons. When you run a command cd with a relative path (i.e., without a leading slash), by default the shell searches your local folder for matching names. CDPATH will search the paths you provided for the directory you want to navigate to.

If you set it CDPATH like this:

$ CDPATH=/:/lib

and then enter:

$ cd /home
$ cd tmp

you will always end up in /tmp regardless of where you are.

However, be careful, because if you do not list the local (.) folder, you will not be able to create any other folder tmp and navigate to it as usual:

$ cd /home
$ mkdir tmp
$ cd tmp
$ pwd
/tmp

Oops!

This seems to be the confusion I felt when I realized that the local folder was not included in the more familiar variable PATH… but you need to do this in the PATH variable, because you can be misled by running a fake command from some downloaded code.

Mine is set with a starting point:

CDPATH=.:/space:/etc:/var/lib:/usr/share:/opt

4) SHLVL

Have you ever wondered, entering sigreturn Will it take you out of the current bash shell to another 'parent' shell, or will it simply close the console window completely?

This variable tracks how deep you are in the bash shell. If you create a new terminal, it is set to 1:

$ echo $SHLVL
1

Then, if you launch another shell process, the number increases:

$ bash
$ echo $SHLVL
2

This can be very useful in scripts where you are unsure whether to exit or not, or to track where you are in terms of depth.

5) LINENO

The variable is also useful for analyzing the current status and debugging, LINENOwhich reports the number of commands executed in the session up to now:

$ bash
$ echo $LINENO
1
$ echo $LINENO
2

This is most often used when debugging scripts. By inserting lines such as echo DEBUG:$LINENO, you can quickly identify where in the script you are (or are not).

6) REPLY

If, like me, you usually write code like this:

$ read input
echo do something with $input

then it might come as a surprise that you don't need to worry about creating a variable at all:

$ read
echo do something with $REPLY

This does the same thing.

7) TMOUT

To avoid staying on production servers too long for security reasons or accidentally running something dangerous in the wrong terminal, setting this variable acts as a safeguard.

If nothing is entered within the set number of seconds, the shell exits.

In other words, this is an alternative to sleep 1 && exit:

$ TMOUT=1

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster