You are not logged in.

#1 2023-05-27 11:28:31

D3vil0p3r
Member
Registered: 2022-11-05
Posts: 205

[SOLVED] Checking if I am in TTY or GUI environment (Terminal)

Hello,
I'm using the latest Arch Linux and I was trying to understand how I can detect if I'm working in TTY or GUI environment.

I read several resoruces on Internet, for example https://unix.stackexchange.com/question … a-terminal but in my case, when I write "test -t 0 -a -t 1 -a -t 2" while I have on TTY environment (CTRL+ALT+F2), I don't get any output.

Why would I like to know this? Because I would like to change the SHELL prompt in case I'm in TTY or I'm using a terminal. I'm using BASH and FISH as shell.

For BASH, I was thinking to put an if statement on .bashrc for PS1, but, it seems I'm not able to differentiate the two environments.
For FISH, I was thinking to use isatty (as explained in its docs https://fishshell.com/docs/current/cmds/isatty.html) but on TTY each example command on the docs I run, it does not produce any output.

Is there an efficient way to do this?

Last edited by D3vil0p3r (2023-05-27 12:59:23)

Offline

#2 2023-05-27 11:37:20

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 9,003
Website

Re: [SOLVED] Checking if I am in TTY or GUI environment (Terminal)

Use tty(1). A GUI shell will return /dev/pts/whatever whereas a console will return /dev/ttyX.

EDIT: but ssh & co. will return /dev/pts/, if that matters.

Last edited by Head_on_a_Stick (2023-05-27 11:42:16)


Jin, Jîyan, Azadî

Offline

#3 2023-05-27 12:26:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,471
Website

Re: [SOLVED] Checking if I am in TTY or GUI environment (Terminal)

D3vil0p3r wrote:

... when I write "test -t 0 -a -t 1 -a -t 2" while I have on TTY environment (CTRL+ALT+F2), I don't get any output.

What output do you expect to get?  Did you take a moment to think about what that command does before you ran it (or read the very simple and clear man page for test)?  `test` tests, it doesn't generate output but just returns an exit status.  But that's a pretty useless test anyways - it says nothing about whether the terminal is an actuall vt vs a (gui) pty - it will return a true value in both cases if all the standard streams have not been closed / redirected, and it will return false in both cases if any of the streams have been closed / redirected.  To be fair, this is exactly what the questioner of the linked thread was looking for - but it's not at all related to your question.

Use tty as noted above.

Last edited by Trilby (2023-05-27 12:38:28)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2023-05-27 12:59:09

D3vil0p3r
Member
Registered: 2022-11-05
Posts: 205

Re: [SOLVED] Checking if I am in TTY or GUI environment (Terminal)

Hello. I solved the issue by using the suggestion of @Head_on_a_Stick

Thank you guys.

Offline

Board footer

Powered by FluxBB