I have a bash script that uses expect inside the bash script. This bash script will prompt for a password inside the expect part of the script and then return some output. I want to take that output and parse it so that it only displays a part of the output to me and stores that in a variable that the normal bash script can use. My script is:


/usr/bin/expect << EOF
spawn -noecho generate passcode -a 123456789
expect "passphrase:" { send "password\n" }
expect eof
) | awk '/^ [a-zA-Z]*: (\S+)/'

echo $output

The output that I get without the awk regex is: Passcode: 543245324532455464 My regex will parse the output and only store the 543245324532455464 value in the output variable. That is what I hoped it would do. If I append the | awk '/^ [a-zA-Z]*: (\S+)/' to the end of the command, it returns nothing, just blank. If I take it off, it returns the output I expect.

Can anyone tell me how I can get the correct data stored into my variable in bash?

  • Great, thanks a lot! That was it.
    – Joe
    Apr 1, 2020 at 18:40

1 Answer 1


As written, you capture the output of /usr/bin/expect then pipe nothing to awk - the closing parenthesis of the command substitution needs to go at the end of the pipeline


... ) | awk '/^ [a-zA-Z]*: (\S+)/'


... | awk '/^ [a-zA-Z]*: (\S+)/')

Be aware that \S is non-standard (PCRE) and may not be supported in all versions of awk and that the capture group likely won't have any effect - you might do better with something like

awk -F': ' '$1 ~ /Passcode/ {print $2}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.