Not sure what is happening.

Lets call script a.sh with the following

/users/guru$ cat a.sh
#! /usr/bin/ksh

echo "Hello"

Execute with ksh. It gets done.

/users/guru$ ksh a.sh
Tue Jul 15 15:00:52 EDT 2014

Ensure file permission and ksh path.

/users/guru$ ls -l a.sh
-rwxrwxrwx    1 guru  kpc         35 Jul 15 15:00 a.sh
/users/guru$ a.sh
ksh: a.sh:  not found
/users/guru$ which ksh
  • Sometimes it is possible to have whitespaces which might cause the error. See the answer to this question here. unix.stackexchange.com/questions/134581/… – Ramesh Jul 15 '14 at 19:22
  • The following command fixed it: add export PATH=$PATH:. to users .profile – Guru Jul 15 '14 at 22:09
  • 1
    See my other comment about why PATH=$PATH:. is considered harmful. – ctrl-alt-delor Jul 15 '14 at 22:12
  • Thanks, Richard. It helps. However, I would ask for more reasons/detials, I do not agree a mere filename-command-conflict warrant banning inclusion of "." to PATH altogether. Simply, I would never name my scripts as a keyword (or ensure "." is at the end of path). – Guru Jul 15 '14 at 22:40

Try "./a.sh" when trying to execute it. It needs to know where the file is at.

The './' tells it to look in the current directory.

  • +1 OK. ./ does the trick. But why so? I should be able to execute the script as such calling the file? – Guru Jul 15 '14 at 19:20
  • 1
    Shell environments have an environment variable called "PATH" (echo $PATH), if the script you are executing is not within the $PATH, then it won't know where to look. If you want to make it executable from anywhere try putting it in a directory in the path, or add a directory to it, where you keep your scripts. – Tango Bravo Jul 15 '14 at 19:22
  • 3
    It used to be common practice to have . in the PATH, but then it was realised that it was a security risk, so it was removed. Imagine . being at start of PATH. Then a local script called ls could intercept calls to ls. If . is but at the end of PATH, then you install package that has a program with conflicting name the this program is run instead of the one you expect. (You may notice that in cmd on ms-windows . is implicitly in the PATH, this could be considered a bug in cmd, as it can not be changed. You may also note that ms-windows has more virus problems. – ctrl-alt-delor Jul 15 '14 at 22:11

When you type a command, the shell looks up the command from a list of directories, as specified by the PATH variable.

The current directory is not in PATH by default (for security reason), so the shell can not find your script.

Using ./, meaning the current directory, so the shell knows where is your script.

  • So, add "." to path and export path? I can use ./ but want to know why can't we run the script as such. – Guru Jul 15 '14 at 19:39
  • Yes, add PATH=$PATH:. in your .bashrc, and remember to do it for TESTING only. – cuonglm Jul 15 '14 at 19:42
  • @Guru You cant run it as such because the directory the script is located in is not in the PATH. Executing a.sh will only work if it can be found in your PATH. – casey Jul 16 '14 at 0:37

As I have posted in the other question, sometimes it is possible to get the file not found error even when you execute the script as ./scriptname. As I have posted in the other answer, you can test it in your machine.


cat ksh_experiment.ksh

echo "Hello"

Now after providing the permissions when I ran the file, it produced the output successfully. Now as discussed over here, I inserted some carriage returns in my file. Now when I ran the script, I was getting the output as,

ksh: ./ksh_experiment.ksh: not found [No such file or directory]

Now, cat -v ksh_experiment.ksh too produced the same output. Also, if I typed vim ksh_experiment.ksh , a new file was getting opened.

As discussed in the answer of the link that I provided, I removed the carriage returns using the command,

 perl -p -i -e "s/\r//g" ksh_experiment.ksh

After fixing when I ran, I got the output as expected.

Now, you can use dos2unix as well to convert the file.

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.