12

I have the following script

#!/usr/bin/Rscript

print ("shebang works")

in a file called shebang.r. When I run it from command line using Rscript it works

$ Rscript shebang.r

but when I run it from the command line alone

$ shebang.r

It doesn't work. shebang.r command not found.

If I type (based on other examples I've seen)

$ ./shebang.r

I get permission denied.

Yes, Rscript is located in /usr/bin directory

Milktrader
  • 9,278
  • 12
  • 51
  • 69

2 Answers2

13

Make the file executable.

chmod 755 shebang.r
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • It worked! Are there other options besides 755 that work? I'm planning to sharpie the command on my laptop. Thanks. – Milktrader Jun 27 '10 at 17:21
  • @Milktrader: what is necessary is that the person trying to run the file have execute permission on it. Read the man page for `chown` and the section of the `ls` man page on the "Long format" (i.e. `ls -l`). – dmckee --- ex-moderator kitten Jun 27 '10 at 17:31
  • Anything that sets the executable bit for the owner should work, as long as you are the owner of the file. Minimally, you need `100`, but then you won't be able to read or write it anymore. You probably want at least `700`, which gives the owner read, write and execute permissions, but denies all permissions to everyone else (except root). – Thomas Jun 27 '10 at 17:31
  • thanks all for the chmod MAN page reference. It has all the info I need – Milktrader Jun 27 '10 at 17:40
2

In addition to Sjoerd's answer... Only the directories listed in the environment variable PATH are inspected for commands to run. You need to type ./shebang.r (as opposed to just shebang.r) if the current directory, known as ., is not in your PATH.

To inspect PATH, type

echo $PATH

To add . to PATH, type

export PATH="$PATH:."

You can add this line to your ~/.bashrc to make it happen automatically if you open a new shell.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • yes, I do need to use ./shebang.r to get it to work. I was told that adding . to the PATH variable introduces security risk, but I might do it anyway since it's my personal computer and it's not like I have missile launch codes on it. – Milktrader Jun 27 '10 at 17:39
  • 2
    The security risk is that you might be in a directory where someone else put, say, a program named `ls` that you might accidentally execute. If `.` is last in your `PATH`, the risk is pretty small, because the usual `/bin/ls` will take precedence, and `.` will only be searched in the case of a nonexistent command. Still, I'd recommend against adding `.` to the `PATH` of the root user, because the potential damage is greater. See further https://listman.redhat.com/archives/redhat-list/1999-July/msg01969.html – Thomas Jun 27 '10 at 17:53