Hi everyone,
I'm sure that most linux users have their own scripts somewhere in
$PATH, like ~/.local/bin dir. I have many too. Some of them are to be run inside terminal and others are to be run directly from dmenu/bemenu (in my case, bemenu because using wayland). Later, I notice that the scripts should be able to determine themself whether should continue or not if it was executed inside terminal or from bemenu. It is because some scripts consist of interactive commands like
read -p "type input: " and need to be run inside terminal and some are not.
In that case, how can I make my scripts to determine whether should continue or not?
According to my googling, I tested the following two, but not work. Both of them return "inside terminal" either running inside terminal or from bemenu.
Code:
#!/bin/bash
tty -s
if [ "0" == "$?" ]; then
echo "inside terminal"
else
echo "no terminal"
fi
Code:
#!/bin/bash
if [ -t 0 ]; then
echo "inside terminal"
else
echo "no terminal"
fi