简体   繁体   中英

"Command not found" inside shell script

I have a shell script on a mac (OSX 10.9) named msii810161816_TMP_CMD with the following content.

matlab

When I execute it, I get

./msii810161816_TMP_CMD: line 1: matlab: command not found

However, when I type matlab into the shell directly it starts as normal. How can it be that the same command works inside the shell but not inside a shell script? I copy-pasted the command directly from the script into the shell and it worked ...

PS: When I replace the content of the script with

echo matlab

I get the desired result, so I can definitely execute the shell script (I use ./msii810161816_TMP_CMD)

Thanks guys!

By default, aliases are not expanded in non-interactive shells, which is what shell scripts are. Aliases are intended to be used by a person at the keyboard as a typing aid.

If your goal is to not have to type the full path to matlab , instead of creating an alias you should modify your $PATH . Add /Applications/MATLAB_R2014a.app/bin to your $PATH environment variable and then both you and your shell scripts will be able to simply say

matlab

This is because, as commenters have stated, the PATH variable inside of the shell executing the script does not include the directory containing the matlab executable.

When a command name is used, like "matlab", your shell looks at every directory in the PATH in order, searching for one containing an executable file with the name "matlab".


Without going into too much detail, the PATH is determined by the shell being invoked. When you execute bash , it combines a global setting for basic directories that must be in the PATH with any settings in your ~/.bashrc which alter the PATH .

Most likely, you are not running your script in a shell where the PATH includes matlab 's directory. To verify this, you can take the following steps:

  • Run which matlab . This will show you the path to the matlab executable.
  • Run echo "$PATH" . This will show you your current PATH settings. Note that the directory from which matlab is included in the colon-separated list.
  • Add a line to the beginning of your script that does echo "$PATH" . Note that the directory from which matlab is not included.

To resolve this, ensure that your script is executed in a shell that has the needed directory in the PATH . You can do this a few ways, but the two most highly recommended ones would be

  1. Add a shebang line to the start of your script. Assuming that you want to run it with bash , do #!/bin/bash or whatever the path to your bash interpreter is. The shebang line is not actually fully standardized by POSIX, so BSD-derived systems like OSX will happily handle multiple arguments to the shebanged executable, while Linux systems pass at most one argument. In spite of this, the shebang is an easy and simple way to document what should be used to execute the script, so it's a good solution.

  2. Explicitly invoke your script with a shell as its interpreter, as in bash myscript.sh or tcsh myscript.sh or even sh myscript.sh This is not incompatible with using a shebang line, and using both is a common practice. I believe that the default shell on OSX is always bash, so you should start by trying with that.


If these instructions don't help, then you'll have to dig deeper to find out why or how the PATH is being altered between the calling context and the script's internal context. Ultimately, this is almost certainly the source of your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM