Exit code of 1 from SSH command

0

Why would the following return a 1:

ssh -tt server_name "sudo grep 'something' /path/to/file || [ $? = 1 ]"

The command in double quotes when run directly on server_name returns 0. Why is the ssh command considered to have failed?

I have the key for the user set up on the remote server. So if I login as userA on serverA, I can simply do the following:

ssh -tt server_name

and login as userA on server_name. So, I know that is not the issue. Is it the -tt? The double quotes?

Touch

Posted 2018-12-13T22:02:05.127

Reputation: 3

Answers

1

$? in double quotes is expanded locally before ssh even starts. The remote server most probably gets

… || [ 0 = 1 ]

and because of this an exit status 1 is possible.

Exchange quotes to pass the literal $? string to the server. It will be expanded on the remote side:

ssh -tt server_name 'sudo grep "something" /path/to/file || [ $? = 1 ]'

Kamil Maciorowski

Posted 2018-12-13T22:02:05.127

Reputation: 38 429

I see, thank you so much for schooling me on that! I literally just thought of removing the quotes before I saw this and it worked. So inverting the quotes prevents expansion of the $?? Is the fact that $? gets expanded in some documentation? I can't find it in the ssh man page. – Touch – 2018-12-13T22:12:14.537

@KingTouchstone It's the shell that expands $?, not ssh. – Kamil Maciorowski – 2018-12-13T22:12:59.753

@KingTouchstone The most general documentation is here. This is what any POSIX compliant shell should obey.

– Kamil Maciorowski – 2018-12-13T22:17:33.407

Thank you for clarifying. I have found the GNU documentation for bash where this is specified, 3.1.2.3 Double Quotes. It seems that using double quotes with grep -P does not affect the regex based on what I understand in that documentation. – Touch – 2018-12-13T22:25:43.243