title: command execute with error when use expect in shell script while work fine in pure shell
body: <p>I try to start all the exited docker containers deployed in separated servers, so basically i should execute the essential command below</p>
  <pre><code>[ $(docker ps -a | grep Exited | wc -l) -ne 0 ] &amp;&amp; docker start $(docker ps -a | grep Exited | cut -d' ' -f1)
  </code></pre>
  <p>It worked fine like in pure linux shell , but then error occured(show below) when i try to use expect to &quot;send&quot; the &quot;essential&quot; command. (local ip is 241,remote end is 209)</p>
  <pre><code>[root@localhost start_shell_dir]# spawn ssh root@192.168.1.209
  root@192.168.1.209's password:
  Last login: Fri Oct 15 22:23:25 2021 from 192.168.1.241
  [root@localhost ~]# invalid command name &quot;0&quot;
      while executing
  &quot;0 -ne 0 &quot;
      invoked from within
  &quot;send &quot;[ 0 -ne 0 ] &amp;&amp; docker start &quot;&quot;
  </code></pre>
  <p>The error log shows i have already log in the remote machine, and something wrong when i execute the docker command.
  <a href="https://stackoverflow.com/users/7552/glenn-jackman">Glenn jackman</a> from the comment area shows me the <a href="https://www.tcl-lang.org/man/tcl8.6/TclCmd/Tcl.htm" rel="nofollow noreferrer">basic rule for tcl</a>,then i realize expect does command substitutions before sending real command. We may see it from execute <code>bash -x script.sh</code> .</p>
  <pre><code>[root@localhost start_shell_dir]# bash -x startContainer.sh
  + read ip pass
  + read ip pass
  + /usr/bin/expect
  [root@localhost start_shell_dir]# ++ docker ps -a
  ++ grep Exited
  ++ wc -l
  ++ docker ps -a
  ++ grep Exited
  ++ cut '-d ' -f1
  spawn ssh root@192.168.1.209
  root@192.168.1.209's password:
  Last login: Fri Oct 15 22:37:56 2021 from 192.168.1.241
  [root@localhost ~]# invalid command name &quot;0&quot;
      while executing
  &quot;0 -ne 0 &quot;
      invoked from within
  &quot;send &quot;[ 0 -ne 0 ] &amp;&amp; docker start &quot;&quot;
  </code></pre>
  <p>Anyway, the final command that work for me is the command showed below(replace double quotes with braces and with backslash before $() to keep it as an ordinary character rather than pre-parse it).</p>
  <pre><code>send {[ \$(docker ps -a | grep Exited | wc -l) -ne 0 ] &amp;&amp; docker start \$(docker ps -a | grep Exited | cut -d' ' -f1)}
  </code></pre>
  <pre><code>#!/bin/bash
  # my original script with error
  while read ip pass
  do
                  {
                  /usr/bin/expect &lt;&lt;-END
                  spawn ssh root@$ip
                  expect {
                  &quot;yes/no&quot; { send &quot;yes\r&quot;;exp_continue }
                  &quot;password:&quot; { send &quot;$pass\r&quot; }
                  }
                  expect &quot;#&quot;
                  send &quot;[ $(docker ps -a | grep Exited | wc -l) -ne 0 ] &amp;&amp; docker start $(docker ps -a | grep Exited | cut -d' ' -f1)&quot;
                  expect eof
                  END
                  }&amp;
  
  done&lt;apps_ip.txt
  </code></pre>