2

I have a private bin folder in my home directory. It's is also in my path. If I try to run a script from anywhere else in my system but within that folder, I get

  sh: 0: Can't open [name of script].sh

If I execute something like sh ~/bin/killwine.sh it works

I would like to be able to execute the script from anywhere without changing my location.

What am I doing wrong?

2

3 Answers 3

4

When you type sh somescript.sh, the system looks for sh in the path not somescript.sh.

Then the system finds sh and passes the argument somescript.sh to it. Then sh looks for somescript.sh in the current folder or in the specified folder ~/bin/ if you typed

sh ~/bin/somescirpt.sh. 

To make the system look for somescript.sh in the path use it directly as a command, without the sh prefix. In other words, type in the terminal:

somescript.sh

For that to work, somescript.sh needs located in a folder in the path, in this case ~/bin/ and it needs to be executable, as others have pointed out. I include the command below for completeness.

chmod +x ~/bin/somescript.sh

Also see How do I run .sh files? and

How to run scripts without typing the full path?

Hope this helps

1
  • Ya know, I've been using Ubuntu and variants as desktop (and sometime server) user for 5 years and this was never clear to me... (and of course I only have learned to use it via school of hard knocks). Thank you...it works :-)
    – Kendor
    Dec 8, 2014 at 19:55
0

As darent and user68186 explained the script may not have the executable bit set. If the bit is set you can run the script if you're in the directory the script is in simply by prefixing it with ./. For example:

./myscript.sh

You don't have to have the .sh at the end of the filename either, but it's good form to append the .sh to let others know it's a shell script.

0

As the comment said, your script may not be marked for execution. When you call it trough sh what you're doing is telling the shell to read its content and execute the functions inside the text file, is not the same as calling an executable directly. Try this:

chmod +x ~/bin/killwine.sh

Then, try to execute it again from another directory. Also, make sure the first line on the script contains this, so the system knows that it has to be executed by a shell:

#!/bin/sh
1
  • 1
    If he's running it on the command line with sh script then he probably wants #!/bin/sh instead.
    – cjm
    Dec 8, 2014 at 20:15

You must log in to answer this question.

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