Using bash shell inside matlab

I am trying to put a large set of bash commands in a matlab script and manage my variables (such as file paths, options, etc.). This is also necessary because this workflow requires manual intervention at certain stages, and I would like to use a step debugger for this.

The problem is that I do not understand how the Matlab interface interacts with the bash shell. I can not do system('source .bash_profile')to define bash variables. In the same way, I cannot define them manually and read them, for example system('export var=somepath'), and then system('echo $var')returns nothing.

What is the correct way to define variables in bash inside a matlab command window? How can I create a workflow of commands that will use the variables defined by me, as well as those contained in my .bash_profile?

+5
source share
2 answers

If all you have to do is set the environment variables, do it in MATLAB:

>> setenv('var','somepath')
>> system('echo $var')
+6
source

Call Bash as the login shell to get the source ~ / .bash_profile file and use the -c option to execute a group of shell commands at a time.

# in Terminal.app
man bash | less -p 'the --login option'
man bash | less -p '-c string'
echo 'export profilevar=myProfileVar' >> ~/.bash_profile

# test in Terminal.app
/bin/bash --login -c '
echo "$0"
echo "$3"
echo "$@"
export var=somepath
echo "$var"
echo "$profilevar"
ps
export | nl
' zero 1 2 3 4 5


# in Matlab
cmd=sprintf('/bin/bash --login -c ''echo "$profilevar"; ps''');
[r,s]=system(cmd);
disp(s);
+3
source

All Articles