Continuing the series of notes on
1) PROMPT_COMMAND
You may already know how to manipulate the prompt to show various useful information, but not everyone knows that you can run a shell command every time the prompt is shown.
In fact, many complex prompt manipulators use this variable to execute commands to collect the information that is 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, the new entries record 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 -
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 call commands.
Like PATH
, variable CDPATH
is a colon-separated list of paths. When you run the command cd
with a relative path (i.e. no leading slash), by default the shell looks in your local folder for matching names. CDPATH
will look in the paths you gave for the directory you want to change to.
If you install CDPATH
in this way:
$ CDPATH=/:/lib
and then enter:
$ cd /home
$ cd tmp
then you will always end up in /tmp
no matter where you are.
However, be careful, because if you do not specify a local (.
) folder, then 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
Whoops!
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 must do it in the PATH variable, because you can be tricked into running a fake command from some downloaded code.
Mine is set by starting point:
CDPATH=.:/space:/etc:/var/lib:/usr/share:/opt
4) SHLVL
Have you ever wondered typing exit
will log you out of the current bash shell into another "parent" shell, or just close the console window completely?
This variable keeps track of how deep you are nested in the bash shell. If you create a new terminal, then it is set to 1:
$ echo $SHLVL
1
Then, if another shell process is started, the number increases:
$ bash
$ echo $SHLVL
2
This can be very useful in scripts where you are not sure whether to exit or not, or keep track of where you are by nesting.
5) LINENO
The variable is also useful for analyzing the current state and debugging. LINENO
, which reports the number of commands executed in the session so far:
$ bash
$ echo $LINENO
1
$ echo $LINENO
2
This is most commonly used when debugging scripts. By inserting lines like 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 $input
it may come as a surprise that you don't have to worry about creating a variable at all:
$ read
echo do something with $REPLY
This does the same.
7) TMOUT
In order not to stay on production servers for too long for security purposes, or accidentally run 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 is exited.
That is, it is an alternative sleep 1 && exit
:
$ TMOUT=1
Source: habr.com