Command paths as variables in shell scripts

What is the best practice when using external utilities in shell scripts. For example, if I want to run "gzip" in several scripts, ensuring that a specific gzip binary is used, should I make the full path to the binary variable and just use the variable if not a script?

GZIP=/home/me/bin/gzip $GZIP somefile $GZIP anotherfile 

Or is it better to practice hard code for each call for binary?

 /home/me/bin/gzip somefile /home/me/bin/gzip anotherfile 

Or is it better to install and rely on the path?

 PATH=/home/me/bin:$PATH gzip somefile gzip anotherfile 

I do not run scripts as root, so maintainability and portability are more important than user security.

+4
source share
2 answers

I think the first option is the best. You will find it or the like:

 SOMEPATH=/path/to/somewhere EXEC_ONE=exec_foo EXEC_TWO=exec_bar $SOMEPATH/$EXEC_ONE --argument baz_file 

in many system scenarios.

You can use conditional expressions to check for availability and even compatibility, available options, etc. and set the variables accordingly.

+2
source

I think the latter is better. If you do not export it, the PATH change will not appear in the programs of your script calls, and you probably do not want this to be visible.

+2
source

Source: https://habr.com/ru/post/1300897/


All Articles