I have written a bash script which calls other process (the ones that ask for password on terminal).

source pc.txt
export snapshot_pw=snp_pw
echo_time() {
    date "+%d:%b:%Y:%H:%M:%S"
# Export Snapshot
echo "$(echo_time) :STARTING EXPORT SNAPPSHOT SCRIPT" | tee -a ${mail_log}
echo "$(echo_time) :ON FAILURE TRY AT MAX ${loop_var} ATTEMPTS" | tee -a ${mail_log}
while [ "${loop_var}" -gt 0 ]; do
expect <(cat << 'EOD'
spawn $::env(Snp_Script_Path)/export_service_instance.sh bootstrap Export_Files/test.bar
expect "Enter RPD Password:"
send -- "$::env(snapshot_pw)\r"
expect "Re-enter RPD Password:"
send -- "$::env(snapshot_pw)\r"
) &> ${export_snapshot_log_location}

Instead of directly hardcoding the password here, how can I read it from within a variable in other file?

It has to be a bash and expect both. I am currently trying to read the file through source and $pass gives password. But within expect, it is not working.

I don't want to encrypt/encode anything. I just want to keep it simple.

  • 1
    Please show what you have tried. I can imagine a few errors that you could have made. But as I can not see what you did, I do not know. Commented Dec 4, 2018 at 22:36
  • First I am reading a text file(It has got pwd=value in it) using source source passcode.txt pass=$pwd and then within expect this pass variable I am unable to read
    – saurav
    Commented Dec 4, 2018 at 22:39
  • 2
    show me the code, (edit question). Commented Dec 4, 2018 at 22:39
  • I have edited and added some snippet
    – saurav
    Commented Dec 4, 2018 at 22:45
  • And which part of the code you added is the minimum to show your problem?
    – RalfFriedl
    Commented Dec 4, 2018 at 23:00

3 Answers 3


I'm not sure of everything that's happening, and what is supposed to be happening, but when you say

export snapshot_pw=snp_pw

you are setting the environment variable snapshot_pw to the string snp_pw.  You probably want to do

export snapshot_pw="$snp_pw"

to set the environment variable snapshot_pw to the value of variable snp_pw, i.e., $snp_pw.

Also, you should quote the shell variable "$mail_log" and any others you use without quoting.  (You don't need the { and }.)


A minimal test case shows that both bash and expect do read the same environment variable:

export foo="bar$$"
echo "bash $foo"

expect <(cat <<'EOD'
puts "tcl  $env(foo)"

which when run shows the same values in both languages:

-bash-4.2$ bash mixin
bash bar7242
tcl  bar7242
-bash-4.2$ bash mixin
bash bar7247
tcl  bar7247

so it is not clear what you mean by "it is not reading" nor what your overall goal is for this code.


You can set a variable based on the contents of a file with a syntax like

snp_pw="$(cat passwd_file)"


$ cat passwd_file 

$ cat x

snp_pw=$(cat passwd_file)

echo Password is $snp_pw

$ ./x
Password is anewpassword

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .