I'm not a programmer, just a chemist. I'm writing a bash script, which I want to run on the server and expect it to do the following:

  1. Open a terminal
  2. Loop over folders slave_1, slave_2, ..., slave_N for each folder open a tab, start up MATLAB (maltab -nodesktop), cd to the folder, and run the script slave_process.

Here is what I have so far:


for i in {1..5}
gnome-terminal --tab  --working-directory="/usr/local/MATLAB/MATLAB_Production_Server/R2015a/bin" --command='./matlab -nodesktop; cd /mnt/projects/slave_$i; -nosplash -r slave_process'

exit 0

Can some help me to get the script to work? I'm not in the sudoers file, so can't change anything on the server.

Update. The answer by @muru does the trick but it starts a new terminal at each loop iteration, which is a pain since I need to run around 200 MATLAB instances. I want to have multiple tabs in the same terminal (or no terminal at all is fine as well). Here is my modified code, which does not work:

for i in {1..5}; do
    cmd=working-directory='/mnt/projects/slave_$i' --command='/usr/local/MATLAB/MATLAB_Production_Server/R2015a/bin/matlab -nodesktop -nosplash -r slave_process'
    args+=(--tab-with-profile=Default -e "sh -c \"$cmd; bash\"")

gnome-terminal "${args[@]}"

Can someone help here?

1 Answer 1



  • Variable expansion doesn't work inside single quotes '' - so slave_$i in the second part of your command will remain slave_$i.
  • Since you know the path of the matlab command, run it directly using the full path, instead of starting in that directory and using ./matlab.
  • If -nosplash -r slave_process are arguments to matlab, they should be in the same command.
  • exit 0 is unnecessary.

Consider something like:


for i in {1..5}
    gnome-terminal --tab  --working-directory="/mnt/projects/slave_$i" \
      --command='/usr/local/MATLAB/MATLAB_Production_Server/R2015a/bin/matlab -nodesktop -nosplash -r slave_process'
  • thank you so much for this. The last question though. Because of {gnome-terminal} on each line, each loop iteration starts a new terminal. How to modify it to have a new tab for each loop iteration in the same terminal? Nov 22, 2015 at 22:24
  • I'm curious - why do you need a terminal at all?
    – muru
    Nov 22, 2015 at 22:27
  • I want to monitor the progress of the script in each folder (I'm doing data fitting and printing out the progress to the window). Could you please, show both versions, with and without any terminal? Nov 22, 2015 at 23:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .