How do I export a variable to the `sh` shell? [duplicate]

Calling export MYVAR=/path/to/whatever from .bashrc obviously works for bash but not for the sh shell. Unfortunately, the Matlab launcher seems hell-bent on using sh and not bash. As such, the simplest way to export an environment variable such that it is accessible from Matlab would be to export said variable to sh.

How can I persistently export an environment variable to sh?

4

1 Answer

Perform the following steps in the current shell (tested with zsh and bash), not in dash:

  1. Open your .profile:

    nano ~/.profile
  2. Add this line

    ENV=$HOME/.dashrc; export ENV
  3. Open .dashrc

    nano ~/.dashrc
  4. Add this line:

    export MYVAR=/path/to/whatever
  5. Finally reload .profile

    . ~/.profile

    or log out and then log in again.

Now start dash with:

  • sh

    or

  • dash

and type

$ echo $MYVAR
/path/to/whatever
5

You Might Also Like