Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6874553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:12:33+00:00 2026-05-27T04:12:33+00:00

I am using expect script inside bash script. When I use foreach inside expect

  • 0

I am using expect script inside bash script. When I use foreach inside expect script, it throws

wrong # args: should be “foreach varList list ?varList list …? command”

The code:

#!/bin/bash

read -p "Enter username: " username
read -s -p "Enter password: " password

#Expect script
/bin/expect -<<EOD 

set SERVERS {100 101 102}

foreach SERVER $SERVERS {
set timeout -1
spawn scp ${username}@plsa${SERVER}.corp.com:/log.2011-11-24 ${SERVER} 
expect "*password:"; send "$password\r"
expect eof }
EOD

echo "completed"

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:12:34+00:00Added an answer on May 27, 2026 at 4:12 am

    The heredoc (<<ENDTOK) is subject to shell expansion on the $variables. That means for each of the $variables you want expect to interpret, you’ll need to escape the $.


    The way to escape something is to prepend a slash to it ($ -> \$).
    It appears the username and password are supposed to come from the shell, the rest from within expect, so here’s how that should go:

    #!/bin/bash
    
    read -p "Enter username: " username
    read -s -p "Enter password: " password
    
    #Expect script
    /bin/expect -<<EOD 
    
    set SERVERS {100 101 102}
    
    foreach SERVER \$SERVERS {
    set timeout -1
    spawn scp ${username}@plsa\${SERVER}.corp.com:/log.2011-11-24 \${SERVER} 
    expect "*password:"; send "$password\r"
    expect eof }
    EOD
    
    echo "completed"
    

    Note the \ in front of $SERVERS and ${SERVER}.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to copy files inside a Python script using the following code: inf,outf
I'm using the following awk command in an expect script to get the gateway
I'm trying to pass a list of arguments to a python script using the
I have an expect file that looks like this. I invoke the script using
How can we copy the files recursively to the remote server using expect script
Since I am using get I expect to see the submitted values appended to
Using any tools which you would expect to find on a nix system (in
Using groovy, would you expect better performance in terms of speed and memory overhead
Is it alright to expect that the user using the back end will have
I'm using NUnit mocks and would like to specify that I expect a call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.