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 thesystemcommand.

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

Create a wrapper filematlab_shell.shin the folderwith the following contents, whereis your custom value.

#!/bin/sh LD_LIBRARY_PATH=export LD_LIBRARY_PATH exec ${SHELL:-/bin/sh} $*

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

From the operating system prompt, call MATLAB settingMATLAB_SHELLto:

/matlab_shell.sh

Display your value ofLD_LIBRARY_PATHfrom the MATLAB command prompt.

!echo $LD_LIBRARY_PATH

Now when you call a function using thesystemcommand, the function uses theLD_LIBRARY_PATHvalue specified by.

See Also