2

Here is the simple script.sh file I have:

#!/bin/bash
pwd
date
ls -lg

I have used chmod +x to make the file executable, but when running it with ./script.sh I get command not found: pwd command not found: date and only the ls -lg command works. I'm a bit perplexed as to why the first two commands aren't working because when I type them into the command terminal they work the way they should.

4
  • 2
    Are there carriage returns in your script? Commented May 3, 2017 at 23:37
  • 2
    Welcome to stackoverflow! Can you edit the post and copy-paste complete and exact error messages? (Just everything from your terminal including yourprompt$ ./script.sh until the script exits.) This is very helpful in file and character encoding issues, because even a missing or additional space, period or colon can hint at what's wrong and where. Commented May 3, 2017 at 23:48
  • 1
    Try cat -A script.sh. This will show you any hidden characters. If cat -A doesn't work, try cat -v. Commented May 4, 2017 at 0:44
  • Maybe your (non-interactive) bash path is all messed up. Try adding some echo's in there, like echo $PATH Commented May 4, 2017 at 4:36

2 Answers 2

4

Also do not use a variable called PATH (cause it is already used :)

Sign up to request clarification or add additional context in comments.

1 Comment

how valuable clue you gave us. I was making mistake and wasn't work my code just because I used a variable with name PATH
3

Maybe there is a (invisible) character there that should not be there. Try typing it again from scratch in the environment that it will run (ex: Linux) or use this command od -c /folder/script.sh to reveal those pesky characters.

1 Comment

Thanks, there did in fact appear to be an invisible character somewhere. I retyped the file from scratch and suddenly it worked just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.