I am trying to determine whether or not I am in a graphical environment via bash on a Mac.

The goal is to have an if/else structure within a bash script that can handle these two cases.

Case 1: I can open windows from the command-line.

Case 2: I cannot open windows from the command-line.

  • You could check whether Finder is running (via ps).
    – nohillside
    Jan 26, 2019 at 6:08

1 Answer 1


Assuming it's enough to check whether Finder is running, you may use

if pgrep -qx Finder; then
    echo 'Finder is running'
    echo 'Finder is not running'

If you are running some X server (not commonly the case on macOS), and want to check whether the current session is attached to it,

if [ -n "$DISPLAY" ]; then
    echo 'In X'
    echo 'Not in X'

Both of these tests are quite naive but would at least cover the most basic situations.

  • Probably needs pgrep -xq Finder, I got two matches without -x. Or even pgrep -qx -u $(id -u) Finder. But as long as the OP doesn't clarify why they need to be able to distinguish this it's not clear whether this is enough.
    – nohillside
    Jan 26, 2019 at 9:01
  • @nohillside Yes, -x may be needed (added that). I'll hold of with further tinkering until we get further feedback.
    – Kusalananda
    Jan 26, 2019 at 9:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .