1

There are many answers about how to add a directory to $PATH. However, I am looking for a way to add it to a path temporarily.

For example, when running a script as sudo, not all scripts have the full directory path, and I would like to add it for one instance, so that the script can run.

  • A wrapper script that adds the directory to path, runs the script you want, and then removes the directory from path would probably work. – music2myear Dec 20 '16 at 17:26
0

A good approach to this is to include paths in the script commands. So rather than

ip addr

You would instead have

/sbin/ip addr

This removes the reliance on the path having the right entries. You can update the path variable in a the script, and it will only exist for the script and any child processes, and revert once they terminate.

export PATH=$PATH:/sbin

This adds /sbin to the existing path.

0

The env command does exactly what you want:

env variable=value command

For the duration of command, $variable is set to value, but the current shell environment is unaffected, and is retained on subsequent commands.

In your case you would want something like:

env PATH=/sbin:$PATH command

Note that env is an external command, so unexported variables and aliases are not accessible while command is running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.