Issue
I like using bash aliases quite often (I am using .zshrc) but I would prefer that the aliases would show what they do. This is because I have to pair program quite often. I know doing type alias_name
and also alias alias_name
displays a description. Is there a way to get my aliases to display their full form before they run? I tried prepending my aliases like alias alias_name='type alias_name && ...'
. However the output for this would also include the prepended code as expected. Is there a way around it?
Solution
In bash
and zsh
you can define a command that prints and executes its arguments. Then use that command in each of your aliases.
printandexecute() {
{ printf Executing; printf ' %q' "$@"; echo; } >&2
"$@"
}
# instead of `alias name="somecommand arg1 arg2"` use
alias myalias="printandexecute somecommand arg1 arg2"
You can even automatically insert the printandexecute
into each alias definition by overriding the alias builtin itself:
printandexecute() {
{ printf Executing; printf ' %q' "$@"; echo; } >&2
"$@"
}
alias() {
for arg; do
[[ "$arg" == *=* ]] &&
arg="${arg%%=*}=printandexecute ${arg#*=}"
builtin alias "$arg"
done
}
# This definition automatically inserts printandexecute
alias myalias="somecommand arg1 arg2"
Example in an an interactive session. $
is the prompt.
$ myalias "string with spaces"
Executing somecommand arg1 arg2 string\ with\ spaces
actual output of somecommand
Answered By - Socowi