Where is the script for alias command in Linux?

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.


Your alias command is most likely a shell-builtin, not a script. You can check this using the type command:

user@host:~$ type alias
alias is a shell builtin

To get documentation about the alias builtin you would look at the bash man page:

man bash

To make an alias persistent you would normally add the command to one of your Bash profile files - most likely your ~/.bashrc file, e.g.:

user@host:~$ echo "alias l='ls -l'" >> ~/.bashrc

Tags:

Bash

Alias