Given that I have the following alias in one of my bash initialization files (.bash_profile etc.):
alias gs='git status'
How can I get my .bash_history file to show the replacement text: 'git status' instead of the alias: 'gs'?
Given that I have the following alias in one of my bash initialization files (.bash_profile etc.):
alias gs='git status'
How can I get my .bash_history file to show the replacement text: 'git status' instead of the alias: 'gs'?
I don't think that this is (easily) possible.
I tried this:
alias foo="echo foo;fc -e /home/hl/tmp/testscript.sh 0"
start cmd:> cat /home/hl/tmp/testscript.sh
#! /bin/bash
set -x
# simple case
#test -f "$1" || exit 1
#echo ": alias replacement : echo foo" >"$1"
# complex case
IFS= read -r line <"$1"
: line: _"$line"_
case "$line" in
foo)
echo ": alias replacement : echo foo" >"$1"
;;
bar)
echo ": alias replacement : echo bar" >"$1"
;;
*)
echo ": alias replacement error" >"$1"
;;
esac
The idea was: Run the actual command and immediately afterwards correct the command history which can be done by the bash builtin fc. There are two problems:
fc allows you to edit the file but executes it afterwards. This can partly be solved by writing a dummy command (e.g. prepend :).
The history is based on lines not on executed commands. Thus the command to be modified is not even on the stack yet. This could maybe be addressed by some evil hack like putting the code into $PS1 but I don't feel like trying...
history builtin
There may be a better way which shares the "not yet on the stack" problem with the above approach. I wasn't aware of the builtin history. This command allows to write and read the history file. Thus this could be done:
sed: sed -i 's/^gs$/git status/' ~/.bash_historyThe history stack problem may be adressed this way (I didn't try, though):
alias foo="echo foo;(sleep 1; history ...) &"
shopt -s cmdhist to any bash initialization file (.bash_profile etc.) will instruct bash to accept entire multiline commands as a single entry in the history list.
shopt
cmdhist and I noticed the builtin history. I extended my answer.
foo with ${BASH_ALIASES[foo]}?