📜 ⬆️ ⬇️

Seven Unexpected Bash Variables

Continuing the series of notes on the lesser-known functions of bash, I’ll show you seven variables that you might not have known.

1) PROMPT_COMMAND


You may already know how to manipulate the prompt prompt to show various useful information, but not everyone knows that you can run a shell command each time the invitation is displayed.

In fact, many complex prompt handlers use this variable to execute commands to collect the information that is displayed in the prompt.
')
Try running it in a new shell, and see what happens with the session:

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

2) HISTTIMEFORMAT


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

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

Once this variable is set, the new entries record the time 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 corresponds to 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 call commands.

Like PATH , the variable CDPATH is a list of paths separated by a colon. When you run the cd with a relative path (that is, without a slash at the beginning), by default, the shell looks for the appropriate names in your local folder. CDPATH will search the paths you gave for the directory where you want to go.

If you install CDPATH like this:

 $ CDPATH=/:/lib 

and then enter:

 $ cd /home $ cd tmp 

then you will always be in /tmp no matter where you are.

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

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

Oops!

This is similar to the confusion I felt when I realized that the local folder was not included in the more familiar PATH variable ... but you have to do it in the PATH variable, because you can be fooled by running a fake command from some downloaded code.

Mine is set as the starting point:

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

4) SHLVL


Have you ever wondered if entering exit will take you from the current bash shell to another “parent” shell or just close the console window completely?

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

 $ echo $SHLVL 1 

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

 $ bash $ echo $SHLVL 2 

This can be very useful in scripts where you are not sure whether to go out or not, or keep track of where you are by nesting.

5) LINENO


Also, the LINENO variable is useful for analyzing the current state and debugging, which reports the number of commands executed in the session to date:

 $ 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 determine where in the script you are (or not).

6) REPLY


If, like me, you usually write this code:

 $ read input echo do something with $input 

This may come as a surprise that you don’t have to worry about creating a variable at all:

 $ read echo do something with $REPLY 

It does the same thing.

7) TMOUT


In order not to stay on the production servers for too long for security reasons or not to accidentally run something dangerous in the wrong terminal, setting this variable acts as protection.

If nothing is entered for the specified number of seconds, it exits the shell.

That is, it is an alternative to sleep 1 && exit :

 $ TMOUT=1 

Source: https://habr.com/ru/post/451492/


All Articles