How to send a string with special characters in a expect script

Hello there,
I am having a problem by sending a string which contains special characters, like dollar sign, into a expect script.  This is a script to change users passwords.  The script is embedded within a bash script in which I am capturing the old and new password for the user, so variables $oldPass and $newPass, used on the expect script, are set previously on the shell script:

echo -e "\nPlease enter your current password: "
stty -echo
read oldPass
echo -e "Please enter your desired password: "
stty -echo
read newPass
echo -e "Please re-enter your desired password: "
stty -echo
read newPass2
stty echo

On the expect script, I use the variables as it:

     expect {
             -re \"word:\" { send \"$oldPass\n\" }
             eof { exit }
             timeout { exit }

but when the old/new passwords have, i.e., a dollar sign, I get this error message (I use $oldPass = my$Test):

can't read "Test": no such variable
    while executing
"set oldPass \"my$Test\""

How can I send a variable on a expect script which contains any special character?  How can I escape an entire variable in a expect script from special characters?

Any help will be sincerely appreciated.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


send "$oldPass\n"
esbfernAuthor Commented:
That's not possible because in the way I am embedding the expect script into the bash script is:



 process=$(expect -c "


 exit 1 ")
 echo "$process"

If I use your solution, I will be breaking the content of the $process variable and I will get error messages on the shell.
I usually use separate script for expect, which begins with

#!/usr/bin/expect --

and call it from bash script.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

esbfernAuthor Commented:
The idea is to have both scripts on the same file for simplicity.   I know how to solve the problem, i.e., with Perl but I would like to make this implementation on a bash script which invokes expect instructions on it.
expect -c << EOF


esbfernAuthor Commented:
thanks for all your comments.   The question I have posted is about how to send special characters, like the dollar sign by using expect.  I am not asking about how to invoke expect within a shell script.  Currently, my script is working correctly, except when sending strings with a dollar sign.  The expect script is 'expecting' a local o global variable when using '$' within the string.   My point is how to instruct the expect script that there is not a variable within the string I just sent.  I am asking the script if for example I send the string my$Test, it send my$Test instead of 'my + variable $Test'.  
Add string to the script

oldPass=$(echo -n $oldPass | sed -e 's/\$/\\\$/g')

before using $oldPass in expect.
It works, but try another special characters - what expect will say...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
esbfernAuthor Commented:
it appears this make the trick!   Thanks a lot.  I will check with other special characters but it seem it is working as expected.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.