Oliver Servín

October 18, 2025

Hunting the right exit code

To run a command on a remote server with Fuse, I upload the script as a .sh file to the server. Then, I use SSH to execute a bash command and use `tee` in a pipeline to write the script output to a .log file. The command looks like this:

bash /home/fuse/.fuse/task-1.sh 2>&1 | tee /home/fuse/.fuse/task-1.log

However, I also wanted to capture the exit code of the script. Because the script runs in a pipeline, I was getting the exit code from `tee`, not the .sh script. Capturing the correct exit code was important since it tells me if the remote script was successful or failed. I found that I could use `${PIPESTATUS[0]}` to get the exit code of the first command in the pipeline. I just needed to rewrite my command to append `exit` with the exit code of the script, like this:

bash /home/fuse/.fuse/task-1.sh 2>&1 | tee /home/fuse/.fuse/task-1.log; exit ${PIPESTATUS[0]}

Now, I finally get the correct exit code of the executed script on the remote server. This allows me to provide feedback to the user if something goes wrong.

About Oliver Servín

Hi! I’m Oliver, creator of Antifuse, Picstome and FotoProfesional. Subscribe below to follow my thoughts on business, design, product development, and other topics I’m exploring. Thanks for visiting and reading.