I have a Bash script I was trying to make to help me run a rather complex command with small changes that it would ask me about through echo and read.
I have found solutions to force it to run a terminal to execute the command, but I'm not interested in that. What I would like it to do is, if I space out and just hit Enter on it in Nautilus (making it run with Run Software), it'll just gently pop up a notification saying "Please run this from a terminal."
I can get the popup to happen — as in I know the command — but I can't get the Bash script to tell if it's being run inside a terminal or not, it seems to always think so. Is it even possible?
Best Answer
From
man bash
under CONDITIONAL EXPRESSIONS: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 overssh
, and that the test (using stdin, not stdout) should therefore be:-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 whereasif [ -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:
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.