Run bash command inside zsh script

Hi,
I would like to run following code in bash inside a zsh script. (In this case is output unfortunately very different if you run it in zsh).

I tried to put "bash" in front of the code but I obtained following error message "bash: do: No such file or directory
" eve though I merged the whole code into one line.
Could anyone help?

bases=`echo A T G C` 

for c1 in $bases
do
    for c2 in $bases 
    do
        for c3 in $bases
        do
        echo $c1$c2$c3
done
done
done
bash -c 'b="A T G C"; for c1 in $b;do for c2 in $b;do for c3 in $b; do echo $c1$c2$c3;done;done;done'

Hope this works/helps...

Update: The commands don't need to be squashed in one line though

bash -c 'b="A T G C"
for c1 in $b; do
 for c2 in $b; do
  for c3 in $b; do
   echo $c1$c2$c3
  done
 done
done'
1 Like

But, of course, if you change:

bases=`echo A T G C`

to:

bases=$(echo A T G C)

your script should work just fine with bash , ksh , or zsh . And, if you use the much simpler and more efficient:

bases="A T G C"

or:

bases='A T G C'

your script should work just fine with any shell based on Bourne shell syntax (such as ash , bash , ksh , zsh , or on almost any UNIX or Linux system /bin/sh .

1 Like

This would be much easier. But if I try it I obtain:

A T G CA T G CA T G C

---------- Post updated at 02:38 AM ---------- Previous update was at 02:37 AM ----------

I apologize. I don't use zsh . I had assumed that it was rejecting the old form of command substitution, but zsh (at least on OS X) does not seem to perform field splitting as required by the standards on the unquoted expansion of a variable. Do you have to use zsh for your script instead of running the entire script with bash or ksh ?

zsh is more user friendly for me, so I usually use it. This command should be a minor part of another zsh script.
The other suggesteg solution works, so I'm OK
bash -c 'b="A T G C"; for c1 in $b;do for c2 in $b;do for c3 in $b; do echo $c1$c2$c3;done;done;done'

It appears that you could make your original script work in zsh without needing to invoke bash using:

bases="A T G C"
set -o shwordsplit
for c1 in $bases
do  for c2 in $bases 
    do  for c3 in $bases
        do  echo $c1$c2$c3
        done
    done
done

And, if you wanted to disable standard shell word splitting after executing the nested loops, you could add:

set +o shwordsplit

after the last done .

1 Like

Without setting SH_WORD_SPILT in zsh, you need to rewrite your nested for loops as follows:

bases="A T G C"
for c1 in ${=bases}
do  for c2 in ${=bases} 
    do  for c3 in ${=bases}
        do  echo $c1$c2$c3
        done
    done
done