You are not logged in.

#1 2007-02-23 10:27:23

wickyd
Member
Registered: 2007-02-21
Posts: 23

[Solved] Adding the current virtual console number to PS1 in .bashrc

Hi everyone

I would like my console prompt to look like this on vc1:
[1 14:25 root /usr/share/kbd/consolefonts]

And I would like my console prompt to look like this on vc2:
[2 14:25 root /usr/share/kbd/consolefonts]

The console prompt on vc3 will start with [3 ... , vc4 will be [4 ... etc.

The time, username and full directory paths are already in place.

I need help on how to add the number at the beginning which corresponds to the current console.

Thank you.

wickyd

Last edited by wickyd (2009-02-10 17:16:24)

Offline

#2 2007-02-23 11:27:52

noriko
Member
From: In My Mind
Registered: 2006-06-09
Posts: 535
Website

Re: [Solved] Adding the current virtual console number to PS1 in .bashrc

you should be able to accomplish this with something like PS1='[$DISPLAY \u@\h \W]\$ ' in your .bashrc


The.Revolution.Is.Coming - - To fight, To hunger, To Resist!

Offline

#3 2007-02-23 12:23:49

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: [Solved] Adding the current virtual console number to PS1 in .bashrc

maybe this would work:

who -m |awk {'print $2'}

Offline

#4 2007-02-23 16:51:35

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: [Solved] Adding the current virtual console number to PS1 in .bashrc

Alternatively

tty

will give you the full device node path.  So something like:

foo=$(tty); echo ${foo##/dev/}

will give you the same output as above.

Offline

#5 2007-02-23 23:53:08

wickyd
Member
Registered: 2007-02-21
Posts: 23

Re: [Solved] Adding the current virtual console number to PS1 in .bashrc

who -m |awk {'print $2'}

works, but it displays vc/1, not 1.

1. How do you get awk to print a substring which only displays 1? who -m | awk {'print substr($2,3)'} did not work.
2. How could I implement this into .bashrc?

foo=$(tty); echo ${foo##/dev/vc/}

gives me exactly what I want.

3. Where can I read more about what you just did? I don't understand this.
4. How can I implement this into .bashrc?

Offline

#6 2007-02-24 00:46:29

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: [Solved] Adding the current virtual console number to PS1 in .bashrc

wickyd wrote:
who -m |awk {'print $2'}

works, but it displays vc/1, not 1.

1. How do you get awk to print a substring which only displays 1? who -m | awk {'print substr($2,3)'} did not work.
2. How could I implement this into .bashrc?

TERM_NO=$(who -m |grep -o /[[:digit:]] |sed 's|/||')

and add $TERM_NO to your PS1 line. But I would use phrakture's example:

wickyd wrote:
foo=$(tty); echo ${foo##/dev/vc/}

gives me exactly what I want.

3. Where can I read more about what you just did? I don't understand this.
4. How can I implement this into .bashrc?

man bash
/##<ENTER>
       ${parameter##word}
              The word is expanded to produce a pattern just  as  in  pathname
              expansion.  If the pattern matches the beginning of the value of
              parameter, then the result of  the  expansion  is  the  expanded
              value of parameter with the shortest matching pattern (the ``#''
              case) or the longest matching pattern (the ``##'' case) deleted.
              If parameter is @ or *, the pattern removal operation is applied
              to each positional parameter in turn, and the expansion  is  the
              resultant  list.   If parameter is an array variable subscripted
              with @ or *, the pattern removal operation is  applied  to  each
              member  of the array in turn, and the expansion is the resultant
              list

Offline

#7 2007-02-24 01:02:38

TomE
Member
Registered: 2005-08-06
Posts: 164

Re: [Solved] Adding the current virtual console number to PS1 in .bashrc

wickyd wrote:
who -m |awk {'print $2'}

works, but it displays vc/1, not 1.

1. How do you get awk to print a substring which only displays 1? who -m | awk {'print substr($2,3)'} did not work.

who | awk '{ split($2,s,/vc\//); print s[2]; }'

Offline

#8 2007-02-25 07:59:19

wickyd
Member
Registered: 2007-02-21
Posts: 23

Re: [Solved] Adding the current virtual console number to PS1 in .bashrc

Thank you for showing me so many different ways to achieve the same result.

I added the following two lines to my .bashrc to achieve the prompt I wanted:

TERMNO=$(foo=$(tty); echo ${foo##/dev/vc/})
PS1='[$TERMNO \A \u \w] '

Offline

Board footer

Powered by FluxBB