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.

jww
  • 97,681
  • 90
  • 411
  • 885
Axel Finkel
  • 31
  • 1
  • 1
  • 4
  • 2
    Are there carriage returns in your script? – codeforester May 03 '17 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. – that other guy May 03 '17 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`. – Keith Thompson May 04 '17 at 00:44
  • Maybe your (non-interactive) bash path is all messed up. Try adding some `echo`'s in there, like `echo $PATH` – Xen2050 May 04 '17 at 04:36

2 Answers2

4

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

Michael Käfer
  • 1,597
  • 2
  • 19
  • 37
  • 1
    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 – uzay95 Aug 13 '21 at 05:35
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.

Jamil Said
  • 2,033
  • 3
  • 15
  • 18
  • 2
    Thanks, there did in fact appear to be an invisible character somewhere. I retyped the file from scratch and suddenly it worked just fine. – Axel Finkel May 04 '17 at 17:14