MATLAB Answers

Translated by

이 페이지의 내용은 영어에서 자동 번역되었습니다. 자동 번역 기능을 해제하려면 "<a class="turn_off_mt" href="#" onclick="window._kiq.push(['set', { 'event': 'Turn off MT' }]);">여기</a>"를 클릭하십시오.


UNIX bash shell in an m file?

Asked by alexander on 14 Feb 2012
Latest activity Edited by Matt J
on 12 Oct 2013
I need to invoke a bash file from matlab which is easy enough to do. However i would like instead to write the bash code in an m file and execute the bash commands. The bash code would invoke a standalone application and pipe some things to it.
Can i simply have all bash commands in a string and use
so it would look something like
system('path/ish -file placeholder -mode text <<XXX...
ect ...


Sign in to comment.


2 Answers

Answer by Walter Roberson
on 14 Feb 2012
 Accepted Answer

You would have to emit newline characters in the string you submit to system(), as MATLAB is not able to construct multi-line quoted strings the way that a shell is.
If you want bash specifically, you need to invoke it by name. The shell used by system() is the $SHELL of the environment, which is not certain to be bash. On the other hand, you should be able to take advantage of the #! magic number that is recognized by unix exec*() calls, by coding the first line of path/ish as
and then no matter which shell is your default shell, bash would be invoked to handle path/ish
sysstr = sprintf('path/ish -file placeholder -mode text <<XXX\na...\nb...\n3...\nect ...\nXXX');


No, but the file "ish" should have
as its first line.
ish is of type executable (application/x-executable) and i cannot edit its source code. However i have found that when i enter unix('/bin/bash','-echo') into the command window i can access the terminal through matlab. When i attempt to call ish or bps though it gives me an error message:
bps: /usr/local/MATLAB/R2011b/sys/os/glnx86/ version `GFORTRAN_1.4' not found (required by bps)
any ideas how to fix this? Thanks in advance
Ah, I was thinking that "ish" was a script.
You have a bit of a problem in that here documents ("<<") are processed by whatever shell system() is using. Some shells have different meanings for here documents. In particular, the way to end a here document is different in csh than it is in bash, Borne shell, Korn shell, or the Open Group Single Unix Standard "sh" shell (sometimes referred to as the POSIX shell.) If you can promise that the user shell will _not_ be csh then the syntax you are using is okay and it will not matter which of those other shells I mentioned is in use. If you want to be certain, though, then you would be better in writing the here document to a file in MATLAB and then redirecting input from that file.
The GFORTRAN_1.4 problem is a gfortran bug in release 4.5, and appears to have been fixed in 4.6 if I read the reports correctly. Please check which gfortran you have installed and see if you have the latest stable version. If you do, then please have a look at the workaround described at

Sign in to comment.

Answer by Kaustubha Govind on 14 Feb 2012

Yes, I think that is exactly how you would do it. Are you running into any trouble doing that?


well the problem is that i have a floating license which is currently in use so i cannot try it yet. I was just checking with the community to see if this was a reasonable thing to try. I am concerned that the pipeline will cause an error.
The command would execute exactly as it would in a shell - so I would first test that line in the shell to make sure it's error free.

Sign in to comment.

Translated by