Terry Sun
Terry Sun

Reputation: 11

SSH Remote command exit code always get 0

I found a strange error when I try to get SSH Remote command exit code. I don't understand why it will get different result and how to explain it. Can someone point out what I'm doing wrong or a better way to capture the exit status of the remote ssh command. Appreciate any help.

local command:

ssh_test/test.sh; echo $?;

+ exit 1 1

ssh command:(Ubuntu 18.04)

ssh 127.0.0.1 "ssh_test/test.sh; echo $?"

+ exit 1 0

test.sh

#!/bin/bash set -x exit 1

Upvotes: 0

Views: 2378

Answers (2)

Terry Sun
Terry Sun

Reputation: 11

I should use single quotation marks to double quotation marks. echo $? will become "0", if I use double quotation marks to execution ssh command.

ssh 127.0.0.1 'ssh_test/test.sh; echo $?'

Upvotes: 1

Desert Scuba
Desert Scuba

Reputation: 177

0 is the exit code being returned from the echo command. If you assign the exit code to a variable this can stop it from being masked with the result of echo.

Upvotes: 1

Related Questions