11

I want to add an argument to bash alias script so that i can just run

alias -p l='ls -l'

and it would get added to my .zshrc permenantly. But i am unable to locate the shell script for alias.

I tried whereis alias, but with no luck. even man alias shows alias has no entry.

I looked in /usr/bin, /usr/share/local/bin but with no luck. Can anyone pointout the location?

EDIT: I have also tried looking in all possible paths by ls $(echo $PATH| tr ':' '\n') | grep alias

Infinity
  • 212
  • 6
    alias is bash builtin command, like cd or for, ... Use man bash. – Ipor Sircer May 31 '18 at 13:22
  • 11
    "...to bash alias...", but "...to my .zshrc..." Which shell are you running? Or do you really want to configure one shell from the other? – ilkkachu May 31 '18 at 13:23
  • But how do i edit it to add another argument ? – Infinity May 31 '18 at 13:23
  • You have to manually add it to your rc file. Whether it's .bashrc or .zshrc – jesse_b May 31 '18 at 13:24
  • @ilkkachu I am using zsh shell. So, i want to add a corresponding entry in .zshrc – Infinity May 31 '18 at 13:24
  • See the first part of this answer a difference between a built-in and external commands is discussed: https://unix.stackexchange.com/a/202326/72304. It's about Bash but it works the same way in zsh. – Arkadiusz Drabczyk May 31 '18 at 13:24
  • So, is there no way i can add argument? – Infinity May 31 '18 at 13:25
  • Maybe a work-around could be to use shell functions. Can it be done them. – Infinity May 31 '18 at 13:26
  • @Infinity: You need to do more research about what an alias is. Also read my comment above. – jesse_b May 31 '18 at 13:27
  • @Jesse_b I read about shell bulletins as suggested by @Arkdiusz\ Drabczyk and i know adding it to my rc file would work. But what if i want to add aliases dynamically. – Infinity May 31 '18 at 13:30
  • What do you mean dynamically? The only way to make them persistent is to add it to one of your shell config files. – jesse_b May 31 '18 at 13:32
  • @Jesse_b By dynamically i meant like whever i write alias -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
  • 2
    That isn't what dynamic means. You only have to manually add it to your rc file one time and it will be available every time you open a new shell. That can be referred to as persistence, not "dynamic" – jesse_b May 31 '18 at 13:35
  • @Jesse_b Ok sorry, will take care about that – Infinity May 31 '18 at 13:36

4 Answers4

17

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.

dhag
  • 15,736
  • 4
  • 55
  • 65
ilkkachu
  • 138,973
2

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
igal
  • 9,886
1

As far as I can tell, zsh does not support persistent aliases.

If you like persistent aliases, I recommend you to check http://schilytools.sourceforge.net/man/man1/bosh.1.html for an implementation of aliases with persistent availability.

For other shells, aliases can only be written as alias commands inside the related .*shrc file.

schily
  • 19,173
0

I am mainly working with the bash and have my aliases in .bashrc.

It is located in my home directory under /home/myuser/.bashrc

WeSee
  • 202