Advantage of $PATH over alias

2019-03-29 06:49发布

问题:

I am relatively new to Linux and Unix. With the help of the internet I finally figured out how $PATH and aliases in my .bashrc work.

But I really couldn't find anything that describes when to use which.

Let's say I installed Python3.3 in Library/Frameworks and the executable is /Library/Frameworks/Python.framework/Versions/3.3/bin/python3, but I want to execute python 3.3 just by typing python3 into my terminal.

When I understand it correctly, there are (at least) three methods to achieve this:

1) I modify $PATH in my .bashrc:

export PATH=/Library/Frameworks/Python.framework/Versions/3.3/bin:${PATH}

2) I set an alias in my .bashrc:

alias python3=/Library/Frameworks/Python.framework/Versions/3.3/bin

3) creating a symbolic link (symlink):

ln -s /Library/Frameworks/Python.framework/Versions/3.3/bin /usr/local/bin

What would you say (from your experience) is the "recommended" way?

回答1:

Putting python3 in your path is the correct way to invoke it anywhere you might find yourself in your filesystem. A symbolic link is the best way to change that command to python and keep your scripts non version dependent (you can run a script that depends on python use the symbolic link and a script that needs python 3.0 specifically use python3, even though on your computer they are the same thing). Symbolic links are still files in your filesystem, so they still need to be in your path.

I only see aliases used when you are trying to create some kind of behavior that is different than the default behavior for a command line utility like an alias for ls that adds -a silently.

Also symbolic links are stored in the filesystem so once created they exist for all other users who log in, while aliases only apply to the logged in user who has defined them. They can also have file permissions applied to them.

Here is a fun article about things you can do to your terminal through your .bash_profile including some great aliases.



回答2:

First, there is no reason to install Python in a /Library/Frameworks/ directory. My suggestion is that (at least for a beginner) you should not add top level directories like your /Library. If you compile it from source code, you should have built it with a standard ./configure (and it probably goes into /usr/local/)

I don't know well about compiling Python from source code, but most Linux source code gets by default ./configure-d to a /usr/local/ prefix so their binary go into /usr/local/bin/ which is often already by default in your PATH

Some Linux distributions have an /etc/profile which indirectly, if the directory $HOME/bin/ exists, adds it inside your PATH; in that case just adding binaries and scripts (or symbolic links) there is the most simple way.

My general advice is to avoid having a very long or very specific PATH. In particular, adding a directory inside your PATH for each product is IMHO a mistake. See e.g. the directory-variables section of GNU coding standards, and keep your PATH quite short. Personally I add programs only in /usr/local/bin/ (system-wide) or in $HOME/bin/, perhaps as symbolic links (so I don't change my PATH since it already contains both /usr/local/bin/ and $HOME/bin).

By past experience having a very long PATH is a nightmare, and slows down your interactive shells



回答3:

Thank you all for your explanations.

As I already said, I am pretty new to Unix and Linux. I just wrote an article about those things (aliases, symlinks $PATH) for my blog for other "newbies". I like to write about those things, because they really interest me, and I want to share my experiences - I hope they are helpful to other people, too. Furthermore, it helps me to deepen my understanding if I have to explain things - and it is a good future reference, too!

It would be nice if you could skim over the article very quickly, and if I got some things wrong, I would be very happy about suggestions!

http://scientific-ocean.com/2013/02/17/an-introduction-to-linuxunix-executables-path-aliases-and-symlinks/



回答4:

I would suggest go for alias which would make it easier for conflicts arising if you different versions of Python. The shell will look up the PATH variable and wherever it matches the executable of Python it will execute it. The alias has to be put in your shell profile like .bash_profile.



标签: linux shell unix