The Computer Oracle

Alias does not "override" PATH entries?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001

--

Chapters
00:00 Alias Does Not &Quot;Override&Quot; Path Entries?
00:21 Answer 1 Score 14
00:43 Accepted Answer Score 21
01:46 Thank you

--

Full question
https://superuser.com/questions/1245540/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash #alias #bashalias

#avk47



ACCEPTED ANSWER

Score 21


The which command returns only executables: it knows nothing about aliases, since it is an external program, and there is no mechanism for passing alias information to a child process.

If you enter the command type -a cp you will see all possible interpretations, in order of preference. This includes any alias, since type is a bash internal command.

It is important to realise that an alias will not be interpreted by a sub-process, such as a script or an interactive editor which has an option to run system commands.

If you make cp a function, then your version will run in scripts, though not from other programs:

cp() { /usr/local/bin/gcp "$@"; }

If you want your cp to work everywhere, add $HOME/bin at the head of your PATH list and point $HOME/bin/cp to point to it:

ln -s /usr/local/bin/gcp $HOME/bin/cp

This makes a symbolic link, though you can make it a slightly more efficient hard link (omit -s), but this will normally need root permissions (sudo ln ...). Creating a function and adding to the PATH variable will be done in one of the bash start-up scripts, with user permissions.




ANSWER 2

Score 14


Aliases are internal to the shell. Other programs won't know about them.

which is not a Bash builtin (it is a builtin in some other shells, e.g. zsh). Since which has no privileged information into Bash's aliases, which just looks through PATH for the given term.

type, on the other hand is a Bash builtin, so it can report on aliases.