Is it better practice to explicitly specify the full path to the command in the shell script?


dave559

I'm discussing with a colleague: Is it better practice, or even necessary, to explicitly specify the full path to a command in a shell script (e.g. install script, cronjob, etc.)?

For example, a script user may have set up their system (or been hacked (in which case, all bets are off)) such that the command of the same name exists elsewhere on the system and is found. Commands on which the script depends earlier in their command search path may lead to unexpected or unfavorable results.

Is it worth specifying the full path to make sure the script uses the expected command, or is this just thinking?

Philip Cullin

It depends on the context, and both options have their pros and cons. All in all, I would avoid absolute paths unless you have a very specific reason.

Invoke using the PATH environment variable

Invoking a command without specifying its absolute path (IE uses the existing path PATHin the directory ) makes the script more portable. This is probably unlikely to be a problem for programs, /binbut definitely true for any command you want to include in it /usr/bin. Different systems can place them in various locations, such as /optor /snap.

To make matters worse, users can configure their paths for a number of reasons. This might include things like installing multiple Java runtimes or python versions. So javahardly any calls should be made /usr/bin/javaas their sysadmins may have installed an incompatible version of your script. It is crucial to allow the user to choose which Java version to use.

Another example is that some distributions don't necessarily follow the conventions you expect. In fact, Archlinux bundles /bin /sbin /usr/bin /usr/sbineverything into one directory with symlinks to other directories ( see here ). This should be backwards compatible, but it shows that some standards are not as ubiquitous as you think.

Note that tools like sudo have specific protections for the built-in PATH environment variable, so it's generally safe to assume that scripts that need to be run with sudo will be safe even if you don't specify an absolute path.


call with absolute path

Generally, absolute paths are only used when there is a specific reason.

The main advantage of using absolute paths is that you have more control over the command being run. Here it is possible to install specific alternative tools designed to override user-specified paths.

You have pointed out security concerns. There are a number of ways that might work, but as mentioned above, sudothe executed command is rarely an issue.

You mentioned cron scripts. Cron's PATH environment variable is known to have problems, as it's often not set as you want ( see here ). In the past I have written scripts with absolute paths to cron. This is more because I don't trust the sys-admin that installed it. My preference is always to run cron jobs with a properly set PATH rather than an absolute path in the script.

Related


Specify Shebang Path in Shell Script

constant fun I am running a VM with Ubuntu. In a very simple script I wrote to define the shebang as: #!/bin/zsh However, zshit also exists in /usr/bin/zsh. Does it matter which path I use? Rusty Shackford I hope you'll find this /usr/bin/zshis a symlink to /b

Specify Shebang Path in Shell Script

constant fun I am running a VM with Ubuntu. In a very simple script I wrote to define the shebang as: #!/bin/zsh However, zshit also exists in /usr/bin/zsh. Does it matter which path I use? Rusty Shackford I hope you'll find this /usr/bin/zshis a symlink to /b

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'

Get full command from shell script

Guoliang Cao I'm looking for a way to access the full command from a shell script, e.g. Suppose I have a script called test.sh. When I run it, the command line is passed to ruby as is (except the script itself is removed). $ test.sh print ENV['HOME'] equivale

Get full command from shell script

Guoliang Cao I'm looking for a way to access the full command from a shell script, e.g. Suppose I have a script called test.sh. When I run it, the command line is passed to ruby as is (except the script itself is removed). $ test.sh print ENV['HOME'] equivale

Windows Shell command to get full path of current directory?

username Is it possible to get the full path of the current working directory using a Windows command line command? Also, how can I store that path in a variable used in the batch file? Trevor Bramber Use cdit without arguments if you are using the shell direc

Windows Shell command to get full path of current directory?

username Is it possible to get the full path of the current working directory using a Windows command line command? Also, how can I store that path in a variable used in the batch file? Trevor Bramber Use cdit without arguments if you are using the shell direc

Specify path in VBS script

Ayy Lmao I searched everywhere but couldn't find any answer. I want the savetofile path to be on the desktop regardless of the username. but i get an error. I think this is the path causing the error. Is there a tip? dim xHttp: Set xHttp = createobject("Micros

Specify path in VBS script

Ayy Lmao I searched everywhere but couldn't find any answer. I want the savetofile path to be on the desktop regardless of the username. but i get an error. I think this is the path causing the error. Is there a tip? dim xHttp: Set xHttp = createobject("Micros

How to explicitly specify file directory in Python script

Tai Wo Shan I'm trying to get my Python script to run directly by typing the filename into the Windows Run dialog, but I haven't been able to do it efficiently. After reading an automatic book on Python, I have two simple scripts: one is a clipboard that shoul

How to explicitly specify file directory in Python script

Tai Wo Shan I'm trying to get my Python script to run directly by typing the filename into the Windows Run dialog, but I haven't been able to do it efficiently. After reading an automatic book on Python, I have two simple scripts: one is a clipboard that shoul

How to explicitly specify file directory in Python script

Tai Wo Shan I'm trying to get my Python script to run directly by typing the filename into the Windows Run dialog, but I haven't been able to do it efficiently. After reading an automatic book on Python, I have two simple scripts: one is a clipboard that shoul

Specify full binding path from nested ItemsControls

nuclear programmer Problem with nested controls of ItemItems when the DataContext of the outermost control changes. The internal controls seem to have been updated to reflect the new DataContext, but it's like some "Ghost" bindings are still bound to the old D

Specify full binding path from nested ItemsControls

nuclear programmer Problem with nested controls of ItemItems when the DataContext of the outermost control changes. The internal controls seem to have been updated to reflect the new DataContext, but it's like some "Ghost" bindings are still bound to the old D

CD with full path to each command

newbie In a shell script, what's a faster or more efficient way to first Cd into a directory and then execute the command or write the full path? For example: it is better to write cd /directory1/then then mkdir subdirectory/or mkdir /directory1/subdirectory/.

CD with full path to each command

newbie In a shell script, what's a faster or more efficient way to first Cd into a directory and then execute the command or write the full path? For example: it is better to write cd /directory1/then then mkdir subdirectory/or mkdir /directory1/subdirectory/.