This is possible in ZSH though is problematic (see below) so should not be used:
zshaddhistory() {
local cmd newcmd
cmd=${1%%$'\n'}
newcmd=${(e)cmd}
if [[ $newcmd != $cmd ]]; then
print -sr -- $cmd
print -sr -- $newcmd
return 1
else
return 0
fi
}
Basically we remove the implicit newline from the potential history
addition, then e expand the command into a new string. If that caused
something to happen, a custom history addition is performed for the
original and new command; otherwise, the regular history mechanism (via
return 0) adds the command to the history.
However! This solution is problematic if any of the expanded $(...) have side-effects, a similar problem to C or LISP macro code being run twice. So, I would not really recommend it for production use, unless you like debugging problems such as a simple counter going awry:
% i=0
% print $((i++))
1
% print $((i++))
3
% print $((i++))
5
% print $((i++))
7
Or multiple writes to an output file, etc.