How can a Bash script tell how it was run?
From man bash
under CONDITIONAL EXPRESSIONS:
-t fd
True if file descriptor fd is open and refers to a terminal.
Assuming fd 1 is standard out, if [ -t 1 ]; then
should work for you. The Advanced Shell Scripting Guide claims that -t
used this way will fail over ssh
, and that the test (using stdin, not stdout) should therefore be:
if [[ -t 0 || -p /dev/stdin ]]
-p
tests if a file exists and is a named pipe. However, I'd note experientially this is not true for me: -p /dev/stdin
fails for both normal terminals and ssh sessions whereas if [ -t 0 ]
(or -t 1
) works in both cases (see also Gilles comments below about issues in that section of the Advanced Shell Scripting Guide).
If the primary issue is a specialized context from which you wish to call the script to behave in a way appropriate to that context, you can sidestep all these technicalities and save your self some fuss by using a wrapper and a custom variable:
!#/bin/bash
export SPECIAL_CONTEXT=1
/path/to/real/script.sh
Call this live_script.sh
or whatever and double click that instead. You could of course accomplish the same thing with command line arguments, but a wrapper would still be needed to make point and click in a GUI file browser work.