Old thread, and one of many, but I've been working on expect
for several days. For anyone who comes across this, I belive I've found a doable solution to the problem of passing bash
variables inside an expect -c
script:
#!/usr/bin/env bash
password="TopSecret"
read -d '' exp << EOF
set user "John Doe"
puts "\$user"
puts "$password"
EOF
expect -c "$exp"
Please note that escaping quotations are typically a cited issue (as @Roberto Reale stated above), which I've solved using a heredoc
EOF
method, before passing the bash-variable-evaluated string to expect -c
. In contrast to escaping quotes, all native expect
variables will need to be escaped with \$
(I'm not here to solve all first-world problems--my afternoon schedule is slightly crammed), but this should greatly simplify the problem with little effort. Let me know if you find any issues with this proof of concept.
tl;tr: Been creating an [expect
] daemon script with user authentication and just figured this out after I spent a whole day creating separated bash/expect scripts, encrypting my prompted password (via bash) with a different /dev/random
salt each iteration, saving the encrypted password to a temp file and passing the salt to the expect script (highly discouraging anyone from easily discovering the password via ps
, but not preventative since the expect script could be replaced). Now I should be able to effectively keep it in memory instead.