Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • I can't see those environment variables on any Unix I have interactive access to. But what you are basically saying is the inverse of the suggestion at the end of the accepted answer: Test for the absense of some variable that is usually declared in an interactive shell. Commented Feb 24, 2022 at 10:54
  • @they Thank you for testing. Does SSH_CONNECTION exist in Unix if you connect to the Terminal through SSH? That's an additional variable which could be used in Linux, but it does not exist if the Terminal on the machine itself is used of course. Commented Feb 26, 2022 at 8:49
  • You could test for PS1, the interactive prompt. This is essentially what another answer already suggests, though. Commented Feb 26, 2022 at 8:54
  • Yes, but this does not cover situations when you execute a script by passing the SSH command as a parameter as mentioned in this answer. Commented Feb 26, 2022 at 10:11
  • Yes, as the answer that you link to mentions, it is safer to give the script an indication that it is running from cron (which is the specific situation that we want to test for) than relying on what the environment usually looks like. Personally, I would run the script with FROM_CRON=true ./myscript in the cron schedule, and then test for FROM_CRON in the script (if [ "${FROM_CRON-false}" = true ]; then ...; fi), rather than abusing the command line options. It's unclear why this has not been suggested so far. (EDIT: Oh, yes, it's in the accepted answer). Commented Feb 26, 2022 at 10:22