alias
is a builtin command, so it doesn't show up as script in any file, or as a function. The type
command will show this:
$ type alias
alias is a shell builtin
But you can still override it. A function with the same name will mask the builtin, unless it's explicitly called with the builtin
builtin.
So, something like this should work:
alias() {
if [ "$1" = "-p" ]; then
echo "-p was given";
shift;
fi;
builtin alias "$@";
}
If you want to print the same alias assignment to a file, you need to be careful to get it quoted right, so that it's usable as input to the shell.
Something like this might do (added right after the shift
in the function), but do test it: printf "alias %q\n" "$@" >> ~/my.alias.file
As for the Bash vs. Zsh issue, I think the above works with both, but I'm not an expert on Zsh.
Incidentally, you may also want to note that Bash's alias
already has a -p
option help alias
says:
Options:
-p print all defined aliases in a reusable format
I don't know if it's any use, since the default behaviour of alias
without arguments is also to print all aliases in a reusable format.
cd
orfor
, ... Useman bash
. – Ipor Sircer May 31 '18 at 13:22rc
file. Whether it's.bashrc
or.zshrc
– jesse_b May 31 '18 at 13:24Bash
but it works the same way inzsh
. – Arkadiusz Drabczyk May 31 '18 at 13:24alias -p l='ls'
, ii automatically goes into .zshrc so that i will not need to write everytime i open a new shell, and also i dont have to manually add an entry. – Infinity May 31 '18 at 13:34