Main Content

Change Environment Variable for Shell Command

This example shows how to substitute a user-specified value for an environment variable value set by MATLAB® when you call a function using the system command.

When you use the system command to call a function, the function inherits the MATLAB environment. To change environment variable values, use a shell wrapper. Use the environment variable MATLAB_SHELL to specify a shell containing your updated variable. This example uses a custom value for the environment variable LD_LIBRARY_PATH.

Create a wrapper file matlab_shell.sh in the folder <PATH_TO_SHELL_SCRIPT> with the following contents, where <MY_LIBRARY_PATH> is your custom value.

#!/bin/sh

LD_LIBRARY_PATH=<MY_LIBRARY_PATH>
export LD_LIBRARY_PATH

exec ${SHELL:-/bin/sh}  $*

If you have a user-defined value for SHELL, the expression ${SHELL:-/bin/sh} uses your SHELL value. Otherwise, MATLAB uses the Bourne shell.

From the operating system prompt, call MATLAB setting MATLAB_SHELL to:

<PATH_TO_SHELL_SCRIPT>/matlab_shell.sh

Display your value of LD_LIBRARY_PATH from the MATLAB command prompt.

!echo $LD_LIBRARY_PATH

Now when you call a function using the system command, the function uses the LD_LIBRARY_PATH value specified by <MY_LIBRARY_PATH>.

See Also