Search code examples
shelltclexpect

How to use expect to spawn with arguments from command line?


Suppose that I call my.exp by

my.exp cmd a b c d ...

I want to pass all the arguments of my.exp to spawn. But I don't find a way to do so. Is there a way to do it with expect? Thanks.

#!/usr/bin/env expect

spawn $argv
...

Solution

  • You'd want to use tcl's argument expansion syntax to do this:

    spawn {*}$argv
    

    Example

    my.exp:

    #!/usr/bin/env expect
    log_user 0
    puts "Spawning '$argv'"
    spawn {*}$argv
    expect *foo* { puts "Got: '[string trim $expect_out(buffer)]'" }
    

    Usage:

    $ ./my.exp echo foo bar
    Spawning 'echo foo bar'
    Got: 'foo bar'