Interactions between Bash Scripts and Linux Command History

Avatar

By squashlabs, Last Updated: Oct. 21, 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/bashecho "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.shHello, 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: Formatting and Displaying Dates with 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 Find Files Based On Wildcard In Linux

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.

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.

Related Article: Securing MySQL Access through Bash Scripts in Linux

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.

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.

Related Article: How to Import JSON from a Bash Script on Linux

Additional Resources



- How to Clear Command History in Bash

How to Extract Substrings in Bash

Extracting substrings in bash can be a powerful tool for manipulating text and data. In this guide, we will explore different methods to extract subs… read more

Using SSH to Connect to a Remote Server in Linux

This article provides a tutorial on using SSH to connect to a remote server in Linux. It covers topics such as the basics of SSH, generating and usin… read more

How To Stop A Process Running On A Specific Port In Linux

Guide on terminating a process running on a particular port in Linux. Learn how to stop a process using the lsof and fuser commands. Additionally, fi… 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… read more

Using Variables in If Statements in Bash Scripts

Using variables in if statements in bash scripts allows for dynamic decision-making within the script. This article explores the syntax, examples, be… read more

How to Apply Chmod 777 to a Folder and its Contents in Linux

Applying Chmod 777 permissions to a folder and its contents in Linux can be done easily by following a few simple steps. This step-by-step guide will… read more

How to Choose the Preferred Bash Shebang in Linux

Choosing the right bash shebang in the Linux environment can greatly impact the performance and compatibility of your scripts. This simple guide pres… read more

How To Use a .sh File In Linux

Table of Contents 1. Create a .sh File2. Add Commands to the .sh File3. Make the .sh File Executable4. Execute the .sh File5. Understanding Exit Cod… read more

How to Loop Through an Array of Strings in Bash

Looping through an array of strings in Bash is a common task for Linux developers. This article provides a guide on how to accomplish this using two … read more

Comparing Array Sizes in Bash Scripting on Linux

Learn how to compare array sizes in bash scripting on Linux with a variety of code snippets and commands. Discover different approaches such as using… read more