简体   繁体   中英

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

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 .

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 $?'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM