Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

while browsing through hidden files of my home directory , I have found .bash_logout. I came through this content in the file

if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi

I am not much good with BASH & I am requesting that , could somebody explain that for me. whats the deal with SHLVL ?

Thank you.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

That script - .bash_logout - is executed by bash when login shell exits, and that code from your question intends to clear the screen to increase privacy when leaving the console.

SHLVL is a environment variable which came from "SHell LeVeL" and lets you track how many subshells deep your current shell is. In your top-level shell, the value of $SHLV is 1. In the first subshell, it's 2; in a sub-subshell, it's 3; and so on. So SHLVL indicates how many shells deep the user is. If the level is 2, you must type exit, then logout to exit.

So, if "$SHLVL" = 1 i.e. if you are in the top-level shell, then...

[ -x /usr/bin/clear_console ] is another test and means something like: test if the file /usr/bin/clear_console exists and is executable.

Because of && this command: /usr/bin/clear_console -q is executed only if [ -x /usr/bin/clear_console ] succeed with success.

And finally: what /usr/bin/clear_console means? From man clear_console:

clear_console clears your console if this is possible.  It looks in the
environment for the terminal type and then in the terminfo database  to
figure  out  how  to  clear  the  screen.  To clear the buffer, it then
changes the foreground virtual terminal to another  terminal  and  then
back to the original terminal.

clear_console is very approached to clear command which can be used in any terminal/console.

share|improve this answer

From a quick google it seems the SHLVL equates to "SHell LeVeL" and reflects how deep you are with nested shells. I.e. one shell, opens another, which opens another etc..

This value is incremented for each sub-shell opened, so the code is checking to see if this is the top level of the nest - i.e. level 1.

if so, it will then (if /usr/bin/clear_console exists and is executable) run the code to clear the screen.

Taken from here : http://linux.about.com/cs/linux101/g/shlvl.htm

share|improve this answer

If you just want to know why scroll down.

From man bash

SHLVL  Incremented by one each time an instance of bash is started.

But, that's not 100% clear... So let's see:

# echo $SHLVL
1
# ps faux
...
user    4440  0.0  0.9 599220 18796 ?        Sl   08:35   0:00 gnome-terminal
user    4447  0.0  0.2  31912  5304 pts/2    Ss+  08:35   0:00  \_ bash
...
#
# screen
# echo $SHLVL
2
# ps faux
...
user    4440  0.0  0.9 599220 18796 ?        Sl   08:35   0:00 gnome-terminal
user    4447  0.0  0.2  31920  5324 pts/2    Ss   08:35   0:00  \_ bash
user    4772  0.0  0.0  34656  1224 pts/2    S+   08:43   0:00      \_ screen
user    4773  0.0  0.0  34816  1396 ?        Ss   08:43   0:00          \_ SCREEN
user    4774  0.3  0.2  31952  5188 pts/3    Ss+  08:43   0:00              \_ /bin/bash
...
#
# bash
# echo $SHLVL
3
# ps faux
user    4440  0.0  0.9 599220 18796 ?        Sl   08:35   0:00 gnome-terminal
user    4447  0.0  0.2  31920  5324 pts/2    Ss   08:35   0:00  \_ bash
user    4772  0.0  0.0  34656  1224 pts/2    S+   08:43   0:00      \_ screen
user    4773  0.0  0.0  34816  1396 ?        Ss   08:43   0:00          \_ SCREEN
user    4774  0.1  0.2  31952  5348 pts/3    Ss   08:43   0:00              \_ /bin/bash
user    4832  1.3  0.2  31952  5184 pts/3    S+   08:45   0:00                  \_ bash

So the variable increments every time when a bash is nested in another bash. And your code part effects that when the last bash is exiting the screen will be cleared. But it's not been executed in graphical terminal sessions, just in virtual terminals (like when you hit ctrl+alt+f1) and work there.

The reason why is that the next person that works on that virtual terminal cannot see the output of the last screen you had. Because maybe there are critical information on the screen.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.