Create alias or add path?

2

1

I've just installed a new program (let's call it foo) on my Linux machine. The program's files are in an unusual location that is not currently part of my $PATH. If I would like to be able to easily run this program from terminal, while in any directory, is it better to change my $PATH variable or to create an alias? Does it matter?

For example, the alias method would be adding this: alias foo="/path/to/program/foo.sh"

And the $PATH method would be: export PATH=/path/to/program;$PATH

Which is better and why?

Nosrettap

Posted 2013-08-21T17:06:45.667

Reputation: 711

Answers

2

"Better" is a somewhat nebulous term, but I'd recommend adding the program's directory to $PATH, because (unlike aliases) that will be inherited by subprocesses of your shell.

For example, if you start an editor from your shell and then want to run foo.sh from within the editor, as for example to compile the file you're editing or transform its text, then the editor will know where to find foo.sh; if you instead add an alias, you'd need to specify the full path to foo.sh when running it from the editor.

Aaron Miller

Posted 2013-08-21T17:06:45.667

Reputation: 8 849

2

There is a third option. Create a link from one of the directories in $PATH to the program. A new directory can be dedicated just for this purpose (to link to programs in non standard locations). This way PATH will not be polluted if there are more programs like this added in the future and also Aaron mentions, it won't depend on the alias where the alias is not available.

jaychris

Posted 2013-08-21T17:06:45.667

Reputation: 340

1

Absolute path (Here your alias) is more and more faster than set PATH variable..

Because when you set PATH variable for example "/path/to/program" and you want to execute foo.sh shell look at PATH variable and then search foo.sh in that directory (/path/to/program) but when you use absolute path (Here your foo variable), shell won't look at path and then search that directory (/path/to/program) to find "foo.sh".

Sepahrad Salour

Posted 2013-08-21T17:06:45.667

Reputation: 928