Last active
June 15, 2023 09:19
-
-
Save iAnanich/99c76f3134012641e28d8e1a744854ed to your computer and use it in GitHub Desktop.
Enable recording datetime in bash history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This bash script configures the bash history to include timestamps in a human-readable format. | |
# It first checks if the user's .bashrc file exists and contains the HISTTIMEFORMAT configuration. | |
# If the .bashrc file doesn't exist, it creates it and adds the HISTTIMEFORMAT configuration. | |
# If the .bashrc file exists but doesn't contain the HISTTIMEFORMAT configuration, it adds the configuration. | |
# It then applies the changes immediately by sourcing the .bashrc file. | |
# Define the time format for ease of modification and reuse | |
TIME_FORMAT="%y-%m-%d %T | " | |
# Check if the .bashrc file exists in the home directory | |
if [ -f ~/.bashrc ]; then | |
# If .bashrc exists, check if it already contains the HISTTIMEFORMAT configuration | |
if grep -q "HISTTIMEFORMAT" ~/.bashrc; then | |
echo ".bashrc already contains HISTTIMEFORMAT configuration." | |
else | |
# If .bashrc does not contain the HISTTIMEFORMAT configuration, append it to the file | |
echo "export HISTTIMEFORMAT=\"$TIME_FORMAT\"" >> ~/.bashrc | |
echo "HISTTIMEFORMAT configuration added to .bashrc." | |
fi | |
else | |
# If .bashrc does not exist, create it and add the HISTTIMEFORMAT configuration | |
echo "export HISTTIMEFORMAT=\"$TIME_FORMAT\"" > ~/.bashrc | |
echo ".bashrc created and HISTTIMEFORMAT configuration added." | |
fi | |
# Source .bashrc to apply changes immediately without requiring re-login | |
source ~/.bashrc | |
echo "Changes applied, bash history will now include timestamp." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment