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
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:
which matlab
. This will show you the path to the matlab executable.echo "$PATH"
. This will show you your current PATH
settings. Note that the directory from which matlab
is included in the colon-separated list.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
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.
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.
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