365

What is the meaning of return value 127 from $? in UNIX.

Sachin
  • 20,805
  • 32
  • 86
  • 99

10 Answers10

520

Value 127 is returned by /bin/sh when the given command is not found within your PATH system variable and it is not a built-in shell command. In other words, the system doesn't understand your command, because it doesn't know where to find the binary you're trying to call.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
  • 73
    This also happens if a bash script does not have mode "+x" but does indeed exist. – MatthewKremer Mar 04 '14 at 20:48
  • 4
    You can try using `which [program]` to see which binary the OS is using. If it comes up empty, next step is checking execution bit and PATH. – four43 Jun 12 '14 at 16:33
  • 10
    @cr125rider, `which` is not particularly accurate -- it doesn't know about aliases, shell functions, PATH lookup memoization, or other factors internal to shell state. Much better to use `type`, a shell builtin which knows about all of those things. – Charles Duffy Sep 11 '14 at 22:45
  • 7
    This also happened to me with a file that had Windows line feeds. Correcting the line endings to unix format solved the problem – Mitkins Sep 30 '14 at 01:35
  • 4
    @MatthewKremer: Actually, I get `126` (`Permission denied`), not `127` when I attempt to invoke a non-executable file (irrespective of its contents); similarly, an attempt to execute a _directory_ also results in `126` (`is a directory`). – mklement0 Apr 23 '15 at 04:52
  • 1
    ...and this could also mean that there is no support on the system for the architecture of the binary. – Nathan Osman Oct 27 '16 at 20:14
  • 1
    @MatthewKremer You should remove your comment since mklement0 is right. I've just tested by trying to execute a simple text file without the `+x` setting on it and I got the 126 return code. – rbaleksandar Aug 20 '17 at 08:16
76

Generally it means:

127 - command not found

but it can also mean that the command is found,
but a library that is required by the command is NOT found.

okigan
  • 1,559
  • 2
  • 18
  • 33
23

127 - command not found

example: $caat The error message will

bash:

caat: command not found

now you check using echo $?

Victor
  • 3,841
  • 2
  • 37
  • 63
Anitha Mani
  • 863
  • 1
  • 9
  • 17
12

A shell convention is that a successful executable should exit with the value 0. Anything else can be interpreted as a failure of some sort, on part of bash or the executable you that just ran. See also $PIPESTATUS and the EXIT STATUS section of the bash man page:

   For  the shell’s purposes, a command which exits with a zero exit status has succeeded.  An exit status
   of zero indicates success.  A non-zero exit status indicates failure.  When a command terminates  on  a
   fatal signal N, bash uses the value of 128+N as the exit status.
   If  a command is not found, the child process created to execute it returns a status of 127.  If a com-
   mand is found but is not executable, the return status is 126.

   If a command fails because of an error during expansion or redirection, the exit status is greater than
   zero.

   Shell  builtin  commands  return  a  status of 0 (true) if successful, and non-zero (false) if an error
   occurs while they execute.  All builtins return an exit status of 2 to indicate incorrect usage.

   Bash itself returns the exit status of the last command executed, unless  a  syntax  error  occurs,  in
   which case it exits with a non-zero value.  See also the exit builtin command below.
apbianco
  • 174
  • 3
9

It has no special meaning, other than that the last process to exit did so with an exit status of 127.

However, it is also used by bash (assuming you're using bash as a shell) to tell you that the command you tried to execute couldn't be executed (i.e. it couldn't be found). It's unfortunately not immediately deducible though, if the process exited with status 127, or if it couldn't found.

EDIT:
Not immediately deducible, except for the output on the console, but this is stack overflow, so I assume you're doing this in a script.

falstro
  • 34,597
  • 9
  • 72
  • 86
3

If you're trying to run a program using a scripting language, you may need to include the full path of the scripting language and the file to execute. For example:

exec('/usr/local/bin/node /usr/local/lib/node_modules/uglifycss/uglifycss in.css > out.css');
Nick
  • 10,904
  • 10
  • 49
  • 78
2

This error is also at times deceiving. It says file is not found even though the files is indeed present. It could be because of invalid unreadable special characters present in the files that could be caused by the editor you are using. This link might help you in such cases.

-bash: ./my_script: /bin/bash^M: bad interpreter: No such file or directory

The best way to find out if it is this issue is to simple place an echo statement in the entire file and verify if the same error is thrown.

0

If the IBM mainframe JCL has some extra characters or numbers at the end of the name of unix script being called then it can throw such error.

Smart Coder
  • 1,435
  • 19
  • 19
0

In addition to the given answers, note that running a script file with incorrect end-of-line characters could also result in 127 exit code if you use /bin/sh as your shell.

As an example, if you run a shell script with CRLF end-of-line characters in a UNIX-based system and in the /bin/sh shell, it is possible to encounter some errors like the following I've got after running my script named my_test.sh :

$ ./my_test.sh
sh: 2: ./my_test.sh: not found
$ echo $?
127

As a note, using /bin/bash, I got 126 exit code, which is in accordance with gnu.org documentation about the bash :

If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.

Finally, here is the result of running my script in /bin/bash :

arman@Debian-1100:~$ ./my_test.sh
-bash: ./my_test.sh: /bin/bash^M: bad interpreter: No such file or directory
arman@Debian-1100:~$ echo $?
126
armanexplorer
  • 93
  • 2
  • 5
0
  1. go to C:\Program Files\Git\etc
  2. open gitconfig with notepad
  3. change [core] autocrlf = true To [core] autocrlf = false