Less Known Bash Variables

2 minute read

Bash, the popular command-line shell for Unix-like systems, offers a wide range of variables that provide control and customization options for your shell environment. While variables like $HOME and $PATH are widely known and frequently used, there are several lesser-known variables that can be useful in specific scenarios. In this article, we’ll explore some of these lesser-known Bash variables: PROMPT_COMMAND, HISTTIMEFORMAT, SHLVL, REPLY, and TMOUT.

1. PROMPT_COMMAND

PROMPT_COMMAND is an interesting variable that allows you to specify a command to be executed just before the shell displays the primary prompt. This can be particularly useful if you want to dynamically update your prompt based on certain conditions or display additional information.

Here’s an example that sets the PROMPT_COMMAND variable to display the current working directory in the prompt:

PROMPT_COMMAND='echo -n "[$(pwd)] "'

Now, whenever you change directories, the prompt will automatically update to show the current working directory.

2. HISTTIMEFORMAT

By default, Bash keeps a history of the commands you have executed in a file called .bash_history. However, the HISTTIMEFORMAT variable allows you to include timestamps along with each command entry in the history file.

To enable timestamps in the command history, you can set the HISTTIMEFORMAT variable like this:

HISTTIMEFORMAT='%F %T '

Now, when you view your command history using the history command, you will see timestamps indicating when each command was executed.

3. SHLVL

The SHLVL variable holds the current shell level. Each time you start a new shell or run a script that spawns a subshell, the value of SHLVL is incremented. This variable can be useful in scenarios where you need to track the depth of nested shells or scripts.

For example, you can use SHLVL in a script to display a message indicating the current shell level:

#!/bin/bash
echo "Current shell level: $SHLVL"

Running this script multiple times will show different values for SHLVL, depending on how deeply nested the shells are.

4. REPLY

The REPLY variable is used in conjunction with the read command to store user input. When you use read without specifying a variable name, the input is stored in the REPLY variable by default.

Here’s a simple example:

#!/bin/bash
echo -n "Enter your name: "
read
echo "Hello, $REPLY!"

When you run this script and enter your name, it will greet you using the value stored in the REPLY variable.

5. TMOUT

The TMOUT variable allows you to set an idle timeout for the shell session. If the user does not enter any commands within the specified time, the shell will automatically exit.

To set an idle timeout of 300 seconds (5 minutes), you can set the TMOUT variable as follows:

TMOUT=300

This can be particularly useful in situations where you want to enforce session timeouts for security or resource management purposes.

Conclusion

Bash provides a wide range of variables that allow you to customize and control your shell environment. While many variables like $HOME and $PATH are widely known, variables such as PROMPT_COMMAND, HISTTIMEFORMAT, SHLVL, REPLY, and TMOUT offer additional functionality

that can be useful in specific scenarios. By understanding and utilizing these lesser-known variables, you can enhance your Bash scripting and command-line experience.