I am trying to execute a shell script on a computer I have logged into via ssh using expect. I use this on my localhost to run expect: #!/bin/bash HOST=192.168.1.2 USER=”chitti” PASS=”123″ CMD="./shell_script.sh" XYZ=$(expect -c “ spawn ssh $USER@$HOST expect \”password:\” send \”$PASS\r\” expect \”\\\\$\” send \”$CMD\r\” expect -re \”$USER.*\” send \”logout\” “) echo “$XYZ” The shell script runs some commands and at the end of the script creates a gzipped archive with the data collected. However, the program is creating the archive before all the commands in the shell script have executed, and I get a an error message when I try to unarchive. gzip: stdin: unexpected end of file tar: Unexpected EOF in archive tar: Unexpected EOF in archive tar: Error is not recoverable: exiting now I only see some of the data that is supposed to be in the archive, after running tar -xvzf archive.tgz I have tested this shell script extensively outside of expect and it works perfectly. What can I do?