title: TCL/Expect - exec - how to execute program with parameters
body: <p>I am experimenting with TCL command <code>exec</code> in <code>tclsh</code> and here are my results:</p>
  
  <pre><code>% set show_me_dir "ls"
  ls
  % exec $show_me_dir
  VboxSharedFolder
  % set show_me_dir "ls -la"
  ls -la
  % exec $show_me_dir
  couldn't execute "ls -la": no such file or directory
  % set show_me_dir {ls -la}
  ls -la
  % exec $show_me_dir
  couldn't execute "ls -la": no such file or directory
  % ls -la
  total 141
  d---------+ 1 wakatana Domain Users     0 Jan 22 19:12 .
  d---------+ 1 wakatana Domain Users     0 Apr 16  2014 ..
  ----------+ 1 wakatana Domain Users 20214 Jan 23 18:43 .bash_history
  ----------+ 1 wakatana Domain Users  1494 Apr 15  2014 .bash_profile
  ----------+ 1 wakatana Domain Users  7593 Jan 22 19:03 .bashrc
  d---------+ 1 wakatana Domain Users     0 Jan 15 14:56 VboxSharedFolder
  %
  </code></pre>
  
  <p>Can somebody please explain how can I execute command with arguments?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>The following example from <a href="http://www.wellho.net/mouth/3583_Expanding-a-list-of-parameters-in-Tcl-and-eval.html" rel="nofollow">Expanding a list of parameters in Tcl and eval</a> article was big eye opener of what is going on here:</p>
  
  <p>The variable <code>$action</code> is only expanded into the string <code>"piemiddle apple"</code> <strong>AFTER</strong> the command line has been split into its individual parameters:</p>
  
  <pre><code>% set action {piemiddle apple}
  % set $action
  can't read "piemiddle apple": no such variable
  </code></pre>
  
  <p>Result: <code>set</code> command "sees" one argument, equivalent to:</p>
  
  <pre><code>% set {piemiddle apple}
  </code></pre>
  
  <p>The expand operator allows you to specify that a variable is to be expanded <strong>BEFORE</strong> the command line is split into individual parameters:</p>
  
  <pre><code>% set action {piemiddle apple}
  % set {*}$action
  apple
  </code></pre>
  
  <p>Result: <code>set</code> command "sees" two arguments, equivalent to:</p>
  
  <pre><code>% set piemiddle apple
  </code></pre>
  
  <p>In earlier versions of Tcl, the eval command was the recommended alternative and it remains available today.</p>
  
  <pre><code>% set action {piemiddle apple}
  % eval set $action
  apple
  </code></pre>
  
  <p>Another examples which proves functionality of expansion operator:</p>
  
  <pre><code>% set {*}"name Linus"
  Linus
  % puts $name
  Linus
  %
  %
  % set distro Unbuntu
  Unbuntu
  % set {*}"linux $distro"
  Unbuntu
  % puts $linux
  Unbuntu
  %
  %
  </code></pre>
  
  <p>Finally the discovery that <code>exec</code> needs command as it's first argument and first command option as it's second argument etc.</p>
  
  <pre><code>% exec "ls" "-la"
  total 137
  d---------+ 1 wakatana Domain Users     0 Jan 22 19:12 .
  d---------+ 1 wakatana Domain Users     0 Apr 16  2014 ..
  ----------+ 1 wakatana Domain Users 20214 Jan 23 18:43 .bash_history
  ----------+ 1 wakatana Domain Users  1494 Apr 15  2014 .bash_profile
  ----------+ 1 wakatana Domain Users  7593 Jan 22 19:03 .bashrc
  d---------+ 1 wakatana Domain Users     0 Jan 15 14:56 VboxSharedFolder
  %
  %
  % exec "ls -la"
  couldn't execute "ls -la": no such file or directory
  </code></pre>