I am trying to write a bash script using expect. This bash script will reside on a remote server, to be executed at a later date/time. I am using expect to read in a large data file, perform some processing on it, and writing out the bash script with appropriate values. To do I this, I set up variable that will hold the value of the bash script:

set awk_cmd "awk '\{print \\\$3\}'"
if {$reboot==$reboot_N} {
set command5 "echo '#!/bin/bash\n\ndeclare -i last_reboot=\$(w | grep up | grep days | ${awk_cmd})\n\nif (( \$last_reboot !< 8 )); then\necho \"Your ..."' > /root/script.sh

I have obviously redacted a good portion of the echo command. However, the error I'm getting is in the ${awk_cmd} portion. awk uses the format "awk '{print $3}'".

However, I cannot get the single quotes to print to the bash script. The above prints out {print $3} without the quotes, and all attempts at escaping the single quotes in the awk command have failed.

Notice that I am already using double quotes and single quotes in this. Everything in my command works EXCEPT the awk command.

What am I missing?

EDIT: To make it a bit simpler (I hope), I am trying to pass a literal single quote inside a single-quoted string

  • This would probably be easier if you could scp the script Feb 11, 2022 at 23:55

1 Answer 1


I think I've figured it out.

set command5 "echo '#!/bin/bash\n\ndeclare -i last_reboot=\$(w | grep up | grep days | awk '\\\''\{print \$3\}'\\\'')\n\nif....." > script.sh

The above produces the following:


declare -i last_reboot=$(w | grep up | grep days | awk '{print $3}')

if (( $last_reboot >= 7 )); then
echo "Your ..."

I found the answer here. I just had not figured out the correct escape sequence.

You must log in to answer this question.

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