2

This topic has been discussed already but every post I've read just confuses me even more.

For example, take this simple bash hello world script called test. I created it as the user Bill and did a chmod 755 test to make it executable. The script resides in /home/Bill/scripts and I tried doing exports PATH=$PATH:/home/Bill/scripts

While I'm in that directory I can run the script fine using ./test. But if I back out into /home/Bill I get a "-bash: ./test: No file or directory"

And if I switch into root, I can't find the script even though I've added it to my environment path or I think I did using the export command.

I've also read that I can add the script path into my /etc/sudoers and have no password required but this requires the user to run sudo ./test. Is there someway to just make this script universally accessible and executable from any user anywhere?

As another side note, i'm kind of confused on the difference between ./test vs test I created the script as test but when I or any other user runs it do they run it as ./test or just test

0

2 Answers 2

4

You can execute a script either by specifying a path (relative or absolute) to the script, or by ensuring that the directory containing the script is in your shell's search $PATH.

Temporarily ignoring the fact that test is not a good name for this example, as there are other tests your shell may choose to execute, you can always execute the script by name test if the containing directory is in your $PATH. This is what you have done with export PATH=$PATH:/home/Bill/scripts. In the terminal in which you have set this $PATH, you can execute test regardless of your current working directory.

If the script is not in your $PATH, or you want to explicitly specify which test you want to execute, you can provide not just the filename, but a relative or absolute path to the file. So, again regardless of your current working directory, you can always execute the script by providing the full path: /home/Bill/scripts/test.

You can also execute the script by providing a relative path; for some examples, where the prompt indicates your working directory:

/home/Bill/scripts$ ./test
/home/Bill$ scripts/test
/home/Bill/documents$ ../scripts/test
/home/James/documents$ ../../Bill/scripts/test

The reason you receive an error when you tried to execute

/home/Bill$ ./test

is because there is no file /home/Bill/test, which is what you are trying to execute ("the file test in the current directory .")


If you want all users to be able to run the script simply with its name, without giving a absolute or relative path to the script, it must be in everyone's $PATH. You can do this either by having every user add /home/Bill/scripts to their $PATH, or by putting the script in a location that is in the default $PATH for all users, such as /usr/local/bin. But you'll have to call your script something other than test, because test is a special case: it's a shell built-in command.

1

Naming a script "test" is not a good idea because there is a "test" command already:

type test
test is a shell builtin

Beside this the PATH variable always work to find a program or script. So place the path of your script to PATH with

export PATH=/your/place/to/directory_of_script:$PATH

will make your script accessible. But do not use names of shell builtin or otherwise already existing names of programs or scripts!

1
  • please take in mind that user's need also access-rights (use chmod or setfacl) to provide access rights to the script. also you need to think about top level directory access. this is beside the PATH configuration !
    – 0x0C4
    Commented Apr 1, 2017 at 17:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .