$ ssh t@localhost [[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'

I was wondering why the above shell is interactive, given that It has nonoption arguments, and doesn't have -s or -c or -i.

From bash manual:

An interactive shell is one started without non-option arguments, unless -s is specified, without specifying the -c option, and whose input and error output are both connected to terminals (as determined by isatty(3)), or one started with the -i option.



You are not performing the test you think you are. Your expansion of $- is happening before the ssh command is executed. To demonstrate:

$ set -x
$ ssh home echo $-
+ ssh home echo himxBHs
$ ssh home 'echo $-'
+ ssh home 'echo $-'
| improve this answer | |
  • Thanks. How would you quote or escape my command to make it work? – Tim Apr 6 '18 at 16:19
  • Does ssh add bash -c when running the command? – Tim Apr 6 '18 at 16:20
  • @Tim, I would quote it thusly: Either ssh t@localhost '[[ $- == *i* ]] && echo "Interactive" || echo "Not interactive"' to perform the string comparison test remotely, or ssh t@localhost '[[ $- == *i* ]]' && echo 'Interactive' || echo 'Not interactive' to do it locally. – DopeGhoti Apr 6 '18 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.