Skip to main content
12 events
when toggle format what by license comment
Apr 14, 2023 at 16:43 comment added RonJohn @Kamil but sourcing test.sh (understandably) makes it say that it's interactive.
Apr 14, 2023 at 16:41 comment added RonJohn @Kamil you're right. I just tested it, too, and case "$-" in tests for interactive vs script, not "script run from bash prompt" vs "script run from cron job".
Dec 15, 2022 at 1:07 comment added Kamil So... this answer is wrong. Script invoked manually (./test.sh) says that this is not interactive shell. So I cannot determine if this is cron job or script started manually.
May 13, 2021 at 15:30 history edited Tim Kennedy CC BY-SA 4.0
Updated link to bash man pages - new manual splits the references, so added the second link too
Apr 17, 2020 at 15:22 comment added Tim Kennedy @saraedum see this page for more clarity on interactive and non-interactive shells: tldp.org/LDP/abs/html/intandnonint.html
Apr 17, 2020 at 15:21 comment added Tim Kennedy @saraedum when you run bash script.sh, the shell that's invoked isn't allocated a tty, and isn't actually an interactive shell. So this is actually working as intended. See the difference between echo $- and bash -c 'echo $-'.
Apr 16, 2020 at 14:19 comment added saraedum case "$-" in *i*) echo true ;; *) echo false ;; esac says true when I run it directly on the prompt. However, when putting it in a script.sh and running it with bash script.sh it says false. So this does not work for me to detect whether the script is being run from cron or invoked directly on an interactive shell.
May 18, 2017 at 15:51 comment added Tim Kennedy @Hobadee - every bash i have access to has $-, as do dash and ksh. even the restricted shells in Solaris have it. What platform are you trying to use it where it's not working? What does case "$-" in *i*) echo true ;; *) echo false ;; esac show you?
May 17, 2017 at 23:13 comment added Hobadee 'case "$-" in' doesn't appear to work in bash scripts.
Jan 6, 2017 at 4:08 history edited Tim Kennedy CC BY-SA 3.0
Updated URL for bashref_54.html
Nov 17, 2015 at 10:08 comment added mveroone Note that the $PS1 command does not work when checking if script is started by systemd or not. the $- one does
Aug 31, 2012 at 11:50 history answered Tim Kennedy CC BY-SA 3.0