How to pass argument in Expect through the command line in a shell script

#!/usr/bin/expect
set username [lindex $argv 0]
set password [lindex $argv 1]
log_file -a "/tmp/expect.log"
set timeout 600
spawn /anyscript.sh
expect "username: " { send "$username\r" }
expect "password: " { send "$password\r" }
interact

If you want to read from arguments, you can achieve this simply by

set username [lindex $argv 0];
set password [lindex $argv 1];

And print it

send_user "$username $password"

That script will print

$ ./test.exp user1 pass1
user1 pass1

You can use Debug mode

$ ./test.exp -d user1 pass1

A better way might be this:

lassign $argv arg1 arg2 arg3

However, your method should work as well. Check that arg1 is retrieved. For example, with send_user "arg1: $arg1\n".