To show timestamps besides the listed commands when using the Linux history command, we first need to export the HISTTIMEFORMAT environment variable in Linux.

The history command in Linux is a powerful utility that allows users to view and manage the command history of their terminal sessions.

Every command executed in the terminal is stored in a history file, and the history command provides a way to access this list. Users can recall previously used commands, re-execute them, or even search through past commands for efficiency. This command is especially useful for users who frequently work in the terminal, as it saves time and reduces the need to manually retype lengthy or complex commands.





Enable Timestamps for the History Command

Enable timestamps just for the current session and not permanent by creating the HISTTIMEFORMAT environment variable.

# export HISTTIMEFORMAT='%F %T '


Enable timestamps permanent for the current user by placing the HISTTIMEFORMAT environment variable into the bashrc file. The file consists of commands, functions, aliases, and scripts that runs whenever a new terminal session is started in an interactive, non-login shell. It is commonly used on Linux and other Unix-like operating systems.

The .bashrc file stores the Bash shell settings. Modifying this file allows you to change the history command output format.

# echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bashrc

Apply the changes by sourcing the .bashrc file:
# source ~/.bashrc


Enable timestamps permanent for all users.

# echo 'export HISTTIMEFORMAT="%F %T "' >> /etc/bashrc
# source /etc/bashrc

or 

# echo 'HISTTIMEFORMAT="%F %T "' >> /etc/environment
# source /etc/environment


Environment Variables

The behavior of Bash and programs run by it can be influenced by a number of environment variables

An environment variable in Linux is a dynamic value that can affect the behavior of processes or applications running on the system. These variables are typically used to pass configuration information to applications and system processes. For example, environment variables can be used to store information like the user’s home directory, the path where executable files are located, or the default editor.

Common environment variables include:

  • PATH: Specifies the directories where the system looks for executable files.
  • HOME: The home directory of the current user.
  • USER: The username of the current user.
  • SHELL: The path of the current user’s shell.
  • LANG: Specifies the system’s locale settings.


View Environment Variables

To view environment variables on a system we can use either the printenv or env command as shown below. We can also use the set command which also works in Windows when using the Command Prompt.


To view as specific environment variable we can use the echo $<variable name> command.

# echo $HISTTIMEFORMAT


Set Environment Variables temporary

Environment variables can be set temporarily for a session or permanently by modifying shell configuration files.

A temporary environment variable is only set for the duration of the session or until the terminal is closed.

To set an environment variable we can use the export command.

# export VARIABLE_NAME=value
# export My_Variable=matrixpost

This variable will be available in the current session.


Remove (Unset) Environment Variables

To unset an environment variable we can use the unset command.

# unset VARIABLE_NAME=value
# unset My_Variable=matrixpost


Set Environment Variables persistently

To make environment variables persistent, we need to export the variables to the user’s profile script (.bash_profile).

Open the user’s profile script or create one.

# vi ~/.bash_profile

Add the export command for every environment variable you want to persist

export My_Variable=matrixpost




Links

How to display the timestamp for history command?
https://access.redhat.com/solutions/161763

How to Set Environment Variables in Linux
https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-set-environment-variables-in-linux/

Linux environment variable tips and tricks
https://www.redhat.com/sysadmin/linux-environment-variables