Interactions between Bash Scripts and Linux Command History

Avatar

By squashlabs, Last Updated: October 20, 2023

Interactions between Bash Scripts and Linux Command History

Saving Commands in Bash History

In Bash, the command history feature allows users to view and recall previously executed commands. By default, all commands entered in the Bash shell are saved in a history file. This can be useful for quickly reusing or referencing previous commands, but it can also raise concerns about privacy and security, especially when executing commands from a bash script.

When executing commands from a bash script, whether interactively or in a non-interactive session, the commands are typically saved in the history file. However, there are ways to prevent commands from being saved or to exclude specific commands from the history file.

Let’s take a look at some examples to better understand how commands from bash scripts are saved in the history.

Example 1:

Suppose we have a bash script called script.sh that contains the following commands:

#!/bin/bash

echo "Hello, World!"
ls

If we execute this script by running ./script.sh in the terminal, both the echo and ls commands will be saved in the history file.

Example 2:

Now let’s consider a scenario where we use the source command to execute the script instead. The source command, also known as . (dot) command, executes the script in the current shell environment. In this case, the commands from the script will also be saved in the history file.

$ source script.sh
Hello, World!
file1.txt  file2.txt

As you can see, executing commands from a bash script, whether directly or using the source command, will result in the commands being saved in the history file by default.

Related Article: Adding Color to Bash Scripts in Linux

Bash History File Location

To view the contents of the bash history file, you can use a text editor or a command-line tool such as cat or less. For example:

$ cat ~/.bash_history

This will display all the commands saved in the history file, including those executed from bash scripts.

Viewing Command History in Bash

To view the command history in Bash, you can use the history command. This command displays a numbered list of previously executed commands, along with their line numbers.

$ history
  1  echo "Hello, World!"
  2  ls
  3  source script.sh
  4  cat ~/.bash_history
  5  history

The history command shows the most recent commands at the bottom of the list. The line numbers can be used to reference specific commands for re-execution or editing.

Clearing Bash History

If you want to clear the entire command history in Bash, you can use the history command with the -c option. This will remove all the commands from the history file.

$ history -c

After running this command, the history file will be empty, and the command history will no longer be available.

Related Article: How to Calculate the Sum of Inputs in Bash Scripts

Preventing Commands from Bash Scripts from Being Saved in History

To prevent commands from a bash script from being saved in the history, you can use the HISTCONTROL environment variable. This variable allows you to specify various options for controlling the command history behavior.

One option is to set the HISTCONTROL variable to ignorespace. This will cause any command preceded by a space to be excluded from the history. For example:

$ export HISTCONTROL=ignorespace
$  echo "This command will not be saved in history"

In this example, the echo command is preceded by a space, so it will not be saved in the history file.

Alternatively, you can set the HISTCONTROL variable to ignoreboth. This option combines the behavior of ignorespace and ignoredups. The ignoredups option prevents duplicate commands from being saved in the history.

$ export HISTCONTROL=ignoreboth
$ echo "This command will be saved in history"
$ echo "This command will not be saved in history"
$ echo "This command will be saved in history"

In this example, the second echo command is preceded by a space, so it will not be saved in the history file. The third echo command is a duplicate of the first one, so it will also be excluded from the history.

Excluding Specific Commands from Being Saved in Bash History

In addition to preventing commands from bash scripts from being saved in the history, you can also exclude specific commands from being saved. This can be useful for sensitive or confidential commands that you don’t want to be recorded in the history file.

To exclude a specific command from being saved in the history, you can prefix it with a space. For example:

$  echo "This command will not be saved in history"

In this example, the echo command is preceded by a space, so it will not be saved in the history file.

You can also use the HISTIGNORE environment variable to specify a pattern of commands to be ignored. This variable contains a colon-separated list of patterns that match commands to be excluded from the history. For example:

$ export HISTIGNORE="ls:cd"

In this example, the ls and cd commands will be excluded from the history.

Clearing the Entire History of Commands in Bash

To clear the entire history of commands in Bash, you can use the history command with the -c option, as mentioned earlier. This will remove all the commands from the history file.

$ history -c

After running this command, the history file will be empty, and the command history will no longer be available.

Related Article: Locating Largest Memory in Bash Script on Linux

Viewing Only Specific Commands in the Bash History

If you want to view only specific commands in the Bash history, you can use the history command with a pattern argument. This allows you to filter the history based on a specific keyword or command.

$ history | grep "git"

In this example, the history command is piped to the grep command, which filters the output to display only commands that contain the keyword “git”. This can be useful for quickly finding and recalling specific commands from the history.

Increasing the Size Limit of the Bash Command History

$ export HISTSIZE=10000

In this example, the HISTSIZE variable is set to 10000, which increases the size limit of the history to 10000 lines. You can set this variable to any value you prefer.

Disabling the History Feature in Bash

If you don’t want any commands to be saved in the history file, you can disable the history feature in Bash by setting the HISTSIZE variable to 0.

$ export HISTSIZE=0

After running this command, no commands will be saved in the history file, and the command history will be disabled.

Related Article: Terminate Bash Script Loop via Keyboard Interrupt in Linux

Commands’ Immediate Saving in the History File

$ export PROMPT_COMMAND='history -a'

In this example, the PROMPT_COMMAND variable is set to the history -a command, which appends the current command to the history file after each command is executed. This ensures that the history file is updated immediately.

Default Size Limit for the Bash Command History

The default size limit for the Bash command history is determined by the HISTSIZE environment variable. If the HISTSIZE variable is not set, Bash uses a default value.

To check the current value of the HISTSIZE variable, you can use the echo command:

$ echo $HISTSIZE

If the variable is not set, the output will be empty or undefined.

The default size limit for the Bash command history can vary depending on the distribution and configuration. In most cases, it is set to a reasonable value that allows you to access a sufficient number of previous commands.

Additional Resources

How to Clear Command History in Bash

Displaying Memory Usage in Bash Scripts on Linux

Learn how to display memory usage within a bash script on a Linux environment. This article covers various commands such as top, vmstat, sar, and ps, along with... read more

How to Handle Quotes with Md5sum in Bash Scripts

When working with md5sum bash scripts in Linux, it is important to understand how to handle quotes. This article provides a thorough look at the best practices for... read more

Accessing Seconds Since Epoch in Bash Scripts

Detailed instructions on how to access seconds since epoch in bash scripts in a Linux environment. Learn how to convert epoch time to a readable date format, get the... read more

Preventing Terminal Print from Bash Scripts in Linux

Learn how to prevent bash scripts from printing to the terminal in Linux. Discover various methods to redirect, suppress, or disable printing in your scripts. From... read more

Parent Variable Accessibility in Bash Scripts

This tutorial offers an in-depth exploration of how a bash script in Linux can access its parent's variables. The article covers topics such as accessing and modifying... read more

Running a Script within a Bash Script in Linux

Learn how to execute a script within a Bash script in a Linux environment. Discover the syntax for calling a script within a script and how to chain multiple scripts... read more