The advantage of $ PATH over an alias

I am relatively new to Linux and Unix. With the help of the Internet, I finally understood how $PATH and aliases in my .bashrc work.

But I really could not find anything that describes when to use.

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 this correctly, there are (at least) three methods for achieving this:

1) I change $ 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 (symbolic link):

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

What would you say (in your experience) the “recommended” way?

+6
source share
4 answers

Putting python3 in your path is the right way to call it anywhere you might end up on your file system. A symlink is the best way to change this command in python and save your version-independent scripts (you can run a script that depends on using python with a symlink and a script that requires python 3.0, use python3 specifically, although on your computer they are one and the same). Symbolic links are still files on your file system, so they should still be in your path.

I see only aliases that are used when you try to create some kind of behavior that is different from the default behavior for the command line, for example, an alias for ls that adds -a silently.

Symbolic links are also stored in the file system, so after they are created, they exist for all other users who are logged in, and aliases apply only to the registered user who defined them. They may also have file permissions.

Here is an interesting article on what you can do with your terminal through .bash_profile , including some great aliases.

+7
source

Firstly, there is no reason to install Python in the /Library/Frameworks/ directory. My suggestion is that (at least for beginners) you should not add top-level directories such as /Library . If you compile it from the source code, you should create it with the standard ./configure (and it will probably go to /usr/local/ )

I'm not very good at compiling Python from source, but most Linux sources default to ./configure -d for the /usr/local/ prefix, so their binary code goes to /usr/local/bin/ , which is often already used by default in your PATH

Some Linux distributions have /etc/profile , which indirectly, if the $HOME/bin/ directory exists, adds it inside your PATH ; in this case, just adding binary files and scripts (or symbolic links) is the easiest way.

My general advice is to avoid a very long or very specific PATH . In particular, adding a directory to your PATH for each product is an IMHO error. See the directory variable section in the GNU coding standards , and keep PATH short enough. Personally, I only add programs to /usr/local/bin/ (system-wide) or to $HOME/bin/ , possibly as symbolic links (so I don’t change my PATH since it already contains as /usr/local/bin/ and $HOME/bin ).

In the past, having a very long PATH is a nightmare and slows down your interactive shells.

+4
source

I would suggest going for an alias that would ease the conflicts that arise if you were using different versions of Python. The shell will look for the PATH variable, and wherever it matches the Python executable, it will execute it. An alias should be placed in your shell profile, for example .bash_profile .

0
source

Thanks to everyone for your explanation.

As I said, I'm pretty new to Unix and Linux. I just wrote an article about these things (aliases, $ PATH symbolic links) for my blog for other "newbies." I like to write about these things because they really interest me, and I want to share my experience - I hope that they are useful for other people. In addition, it helps me deepen my understanding if I have to explain things - and this is also a good reference to the future!

It would be nice if you could quickly revise the article, and if I had some mistakes, I would be very pleased with the suggestions!

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

0
source

All Articles