Last active
August 25, 2024 07:10
-
-
Save renelink/34066cb9204e5a072e7f4d284011f887 to your computer and use it in GitHub Desktop.
Bash Utilities
This file contains 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
# | |
# You can import this script in your current bash using the following command | |
# source <(curl -s https://gist.githubusercontent.com/renelink/34066cb9204e5a072e7f4d284011f887/raw/bash-utils.sh) | |
# | |
# You can also put these lines in your .bashrc or .bash_profile to automatically install the utils if they do not exist | |
# | |
# ----------------------------- | |
# BASH_UTILS_FILE="${HOME}/.bash-utils.sh" | |
# | |
# if ! [ -f "${BASH_UTILS_FILE}" ]; then | |
# curl -o "$BASH_UTILS_FILE" https://gist.githubusercontent.com/renelink/34066cb9204e5a072e7f4d284011f887/raw/bash-utils.sh | |
# fi | |
# | |
# . "$BASH_UTILS_FILE" | |
# ----------------------------- | |
# | |
# To update the git-utils you can then just execute | |
# $ rm ~/.bash-utils.sh | |
# and reopen the shell | |
# This funciton reads an environment variable's content from stdin so that it will not occur in the shell's history. | |
# It is useful when you need to export a variable with sensitive content like a private gpg key or passphrase. | |
# | |
# Usage: exportSecret ENVIRONMENT_VARIABLE_NAME | |
# | |
# In some situations you have to press CTRL-D two times, e.g. when no new line is entered. | |
# See https://unix.stackexchange.com/questions/701317/when-using-cat-to-create-file-i-have-to-type-ctrld-twice-to-finish-the-input | |
# | |
function exportSecret(){ | |
local env_var_name="$1" | |
if [ -z "${env_var_name}" ]; then | |
>&2 echo "Pass the environment variable name as the first argument." | |
return | |
fi | |
>&2 echo "Enter the secret value and finish with CTRL-D. Abort with CTRL-C." | |
IFS='' read -s -r -d '' ${env_var_name} < <(cat) | |
export ${env_var_name} | |
} | |
# Executes an isolated bash shell that does not load any rc or profile files and uses the PS1 provided by ISOLATED_PS1. | |
# The isolated bash will not record a history. | |
# | |
# This is useful if you want to do operations with sensitive data. Just exit the shell and even sensitive data that | |
# you put in environment variables is deleted. Such as gpg keys or passphrases. | |
# | |
alias bash-isolated='bash --rcfile <(echo "export PS1=\"$ISOLATED_PS1\";unset HISTFILE")' | |
# Change this line to the default isolated bash PS1 | |
export ISOLATED_PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[35m\]$MSYSTEM\[\e[0m\] \[\e[33m\]\w\[\e[0m\] \[\e[31m\](ISOLATED)\[\e[0m\]\n$ ' | |
# Delte a history range. | |
histdel(){ | |
local start="$1" | |
local end="$2" | |
if [ -z "$start" ]; then echo "Usage: histdel START_INDEX [END_INDEX]"; return; fi | |
if [ -z "$end" ]; then | |
end=$(history 1 | awk '{print $1}') | |
fi | |
for h in $(seq $end -1 $start); do | |
history -d $h | |
done | |
history -w # force write | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment