10

Related: How to get the current terminal name?

But I only want the ID of the current terminal, e.g. when tty returns /dev/pts/11 I only want to print 11.

The idea is to add this information in the shell prompt after the username \u in the PS1 variable in my .bashrc file:

Wanted result: username11@localhost:~$

For the moment I have:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u$(tty)@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

which gives: username/dev/pts/11@localhost:~$

3
  • 3
    try $(basename $(tty)) can't test, thus can't post as answer.
    – Archemar
    Commented Mar 30, 2023 at 12:28
  • @Archemar that does in fact work.
    – RonJohn
    Commented Mar 31, 2023 at 3:12
  • I don't have an answer, but I do have curiosity: may I ask why you desire such an unusual prompt? What's your use case?
    – noughtnaut
    Commented Apr 1, 2023 at 19:02

1 Answer 1

15

Use \l, it gives the basename of the shell’s terminal device name:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\l@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

This is similar to the \l getty escape.

You must log in to answer this question.

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