2

So I'm trying to run a simple shell script as seen below but I'm facing an error also seen below.
I've added permissions and everything to the file so, in theory, it should be working?
Shell,

#!/bin/bash
echo "Hello Stack"

Error

As seen this is located in my user profile (./home/pi/) if anyone has any idea why this mightn't be working please do share :) thanks in advance.

UPDATE


Thanks, everyone for the suggestions, I have changed the name of the file to simply test.sh and gave this another shot. In the photo below you can see the contents of test.sh as well as the location and my attempts at giving permission to and running the file. I'm totally lost to why this isn't working?
enter image description here

  • How do you start the script? Are you sure it's in ~/ ? – Dirk Aug 13 '18 at 12:37
  • 3
    it looks like you have a space in the filename try renaming the file - to remove the space. If that does not work please edit your question and include the answer to Dirk's question and the output of ls -la. – Steve Robillard Aug 13 '18 at 12:42
  • @SteveRobillard I have updated the post answering Dirk's question and I have renamed the file. – Charlie Simon Aug 14 '18 at 12:18
4

There are several possibilities, the most likely of which are covered by @SteveRobillard and @Dirk in their comments. I'll throw in a couple of more to try to cover all possibilities:

  1. the file must be in your path; if you're logged in as user pi you can use this from wherever you are in the file system: ~/"hello world.sh"

  2. if there's actually a space in your file hello world.sh, you will need to put that in quotes: ~/"hello world.sh" . Alternatively, you can rename the file, e.g. mv ~/"hello world.sh" ~/helloworld.sh

  3. I know you said you've set permissions, but just for good measure: chmod 755 ~/"hello world.sh" (or chmod 755 ~/helloworld.sh if that's your file name)

  4. if you connect to your pi through an SSH connection from another host, make sure the SSH connection is actually up when you enter the command; in other words, are you commanding your remote host to run this command, or are you commanding your RPi to run this command? (i do this all the time! :P )

ADDITION:

I should have thought of this, but didn't until the question was edited :)

  1. If you've created your script file on a Windows machine, then transferred it to your RPi, that will be a problem [see note 1]. You may see a mysterious error message with ^M in it. This is because Windows uses an additional "line control" character. You don't see it because it's hidden... here's an explanation. There are several solutions to this, but in this case, the easiest is just to re-create the file in the editor on your Raspberry Pi (or another Unix machine, or even a Mac). RPi has a very easy-to-use editor called nano; you start it from the command line by typing nano :)

    For a larger file, or if you want/need to edit your files in Windows for some reason, another solution is to install dos2unix on your RPi as follows:

    sudo apt-get update sudo apt-get upgrade sudo apt-get install dos2unix

    Then, once you have transferred your file from Windows to your RPi, run this command:

    dos2unix test.sh (or whatever your file name is, if it's not test.sh)

This will purge the pesky carriage return <CR> from your script, and your shell won't be confused by it any longer.

note 1. "transferred" means that you have transferred it as a file, or you have "copied-and-pasted" it from Windows to *nix. That pesky carriage return will not simply disappear!

  • 1
    Also, make sure the first line in the script ends in a linefeed without any preceding carriage return. The CR is hard to see if you don't know how to look for it, and can lead to a "bad interpreter: no such file or directory" message when spawned directly from bash; it's possible a GUI launching it will only generate the latter part of the error. – Monty Harder Aug 13 '18 at 19:27
  • 2
    @CharlieSimon: Yes, I omitted a possibility, but have addressed it in my edited answer. To summarize, don't create shell scripts in Windows, then transfer them to your RPi to run; create and edit the shell script on your RPi using nano. – Seamus Aug 14 '18 at 13:44
  • 1
    @MontyHarder: Good catch... you saw that coming :) – Seamus Aug 14 '18 at 15:12
  • 1
    @CharlieSimon: Yes, that (copy and paste) means something. Please try one of the solutions I've recommended (create on RPi, or install & run dos2unix). It will work. – Seamus Aug 15 '18 at 11:04
  • 1
    @CharlieSimon: I've edited the answer again to address the C&P aspect of this... and thanks for pointing this out! Once you "know" something, it becomes easy to forget that, at one time, you didn't know it :) – Seamus Aug 16 '18 at 13:25
1

Just as an addition to Seamus's excellent answer, as you clearly have a space in the file name, what is happening when you call it using

hello world.sh

or

./home/pi/hello world.sh

is that the shell attempts to execute the file ./home/pi/hello and presumes that world.sh is an intended argument to be passed to the hello executable.

Obviously, this is not the case, the ./home/pi/hello executable can not be found and hence the error message

1

The command shell, bash in this case, is reacting to the space.

You can run the command if you escape the space:

~/hello\ world.sh

works for me.

Without escaping the space I get the same error message as you did.

  • The esc char is a good trick; for some reason I never think of using it in a shell command. – Seamus Aug 14 '18 at 15:15

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.