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:~$

s.k
  • 461

1 Answers1

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.

Stephen Kitt
  • 434,908