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?

  • @muru I think Where to declare environment variables? is not sufficiently answer the problem for the dash. – A.B. Apr 24 '15 at 8:36
  • 1
    @A.B. yes it is. /etc/environment and .pam_environment apply everywhere. – muru Apr 24 '15 at 8:49
  • @muru, that's not obvious to everyone, though. This question has the merit of being explicit and narrower in scope. – blz Apr 24 '15 at 9:16
  • 1
    @blz I don't see a narrow scope as a merit. And the solution to lack of obviousness is to make it obvious, not post a new question. – muru Apr 24 '15 at 9:17

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


  • dash

and type

$ echo $MYVAR
| improve this answer | |
  • I followed the above steps, then started sh from my current shell (fish), and typed . ~/.profile, but echo $MYVAR still returns nothing. – blz Apr 24 '15 at 7:50
  • first . ~/.profile, than sh – A.B. Apr 24 '15 at 7:51
  • I think part of the issue is that my user's default shell is fish. Reloading ~/.profile raises errors, which is unsurprising since fish is not fully bash-compliant. How should I proceed? – blz Apr 24 '15 at 8:15
  • Is logout and login an option? – A.B. Apr 24 '15 at 8:16
  • 1
    Edit: I got impatient and logged out/in. Works like a charm. Thanks again! – blz Apr 24 '15 at 8:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.