2

I'm working on Ubuntu 12.4 and I have matlab installed. Typically, in order to invoke matlab from terminal I have to type in '~/MATLAB/bin/matlab'. Obviously this is a bit annoying so I aliased this command by adding

alias matlab='sh ~/MATLAB/bin/matlab'

to .bashrc. Now everything is golden and typing 'matlab' in the terminal (bash) works from any directory.

The problem arises when I'm trying to invoke Matlab from a python script. having a statement like this:

>>> commands.getoutput('matlab')
'sh: 1: matlab: not found'

as it seems the alias is not being recognized. Just to make sure:

>>> commands.getoutput('~/MATLAB/bin/matlab')

works like a charm, and

>>> commands.getoutput('echo $SHELL')
'/bin/bash'

indeed verifies that python is trying to execute the cmd in bash...

Any idea what's happening here? why isn't the alias being recognized? how/can it be fixed?

Thanks!

slayton
  • 20,123
  • 10
  • 60
  • 89
ScienceFriction
  • 1,538
  • 2
  • 18
  • 29

2 Answers2

5

Add the matlab binary path to your PATH environment variable.

PATH=~/MATLAB/bin/:$PATH
export PATH

Then python would find matlab:

>>> commands.getoutput('matlab')

I think commands doesn't know of your shell's current aliases. However environment variables such as PATH persist.

aayoubi
  • 11,285
  • 3
  • 22
  • 20
  • 1
    You'll have to use "export PATH=..." or else python will not pick up the PATH value. – Anton Aug 01 '12 at 14:29
  • changing PATH might add unnecessary commands from `~/MATLAB/bin`. `ln -s ~/MATLAB/bin/matlab matlab` in a directory that is already in PATH might be more suitable here – jfs Aug 01 '12 at 14:36
  • thanks for the answer. Setting PATH and exporting ('export PATH= ...' added to .bashrc) solved the problem. – ScienceFriction Aug 01 '12 at 14:55
  • Thanks! i forgot the `export` keyword :) – aayoubi Aug 01 '12 at 17:01
4

bash only evaluates ~/.bashrc if it's started as an interactive shell. If you have aliases in /.bashrc that you want bash to evaluate when it's run non-interactively, you could try setting the BASH_ENV environment variable from your Python script to point to ~/.bashrc.

The Bash Reference Manual gives some more detail on interactive versus non-interactive shells and on how to use BASH-ENV.

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246