To Change PS1 automatically - Unix

This is a discussion on To Change PS1 automatically - Unix ; I want to change PS1 prompt to my system time. For that I wrote PS1='\T'. But This will get effect only when i press enter. I want to update PS1 to current time without pressing enter? How can i do ...

+ Reply to Thread
Results 1 to 17 of 17

Thread: To Change PS1 automatically

  1. To Change PS1 automatically

    I want to change PS1 prompt to my system time.
    For that I wrote PS1='\T'. But This will get effect only when i press
    enter.
    I want to update PS1 to current time without pressing enter?
    How can i do this?


  2. Re: To Change PS1 automatically

    lak wrote:
    > I want to change PS1 prompt to my system time.
    > For that I wrote PS1='\T'. But This will get effect only when i press
    > enter.
    > I want to update PS1 to current time without pressing enter?
    > How can i do this?
    >

    You need to change its definition in your shell startup file which is in
    your home directory, probably called .bashrc or .login

    Robert

  3. Re: To Change PS1 automatically

    On Oct 31, 11:04 am, lak wrote:
    > I want to change PS1 prompt to my system time.
    > For that I wrote PS1='\T'. But This will get effect only when i press
    > enter.
    > I want to update PS1 to current time without pressing enter?
    > How can i do this?


    You can not do that. Not any shell that I know of will make a separate
    thread and use control characters just to do something totally
    inappropriate and inpractical.

    Cheers,
    Darko


  4. Re: To Change PS1 automatically

    On Oct 31, 3:04 pm, lak wrote:
    > I want to change PS1 prompt to my system time.
    > For that I wrote PS1='\T'. But This will get effect only when i press
    > enter.
    > I want to update PS1 to current time without pressing enter?
    > How can i do this?


    Give the following entry in $HOME/.bash_profile of the user for which
    you want to change the prompt

    export PS1='[\T]$ '

    or if you want to set it for all the users , give the same entry in /
    etc/profile

    Logout and relogin, you'll get the new prompt. To revert to the
    original prompt, remove the entry 4m the file and relogin.

    Meenakshi.


  5. Re: To Change PS1 automatically

    meenakshi wrote:
    > On Oct 31, 3:04 pm, lak wrote:
    >> I want to change PS1 prompt to my system time.
    >> For that I wrote PS1='\T'. But This will get effect only when i press
    >> enter.
    >> I want to update PS1 to current time without pressing enter?
    >> How can i do this?

    >
    > Give the following entry in $HOME/.bash_profile of the user for which
    > you want to change the prompt
    >
    > export PS1='[\T]$ '


    Hmm, I tried that, and as I expected, it didn't somehow make the
    time update "without pressing enter".

    - Logan

  6. Re: To Change PS1 automatically

    Logan Shaw writes:
    >meenakshi wrote:
    >> On Oct 31, 3:04 pm, lak wrote:
    >>> I want to change PS1 prompt to my system time.
    >>> For that I wrote PS1='\T'. But This will get effect only when i press
    >>> enter.
    >>> I want to update PS1 to current time without pressing enter?
    >>> How can i do this?

    >>
    >> Give the following entry in $HOME/.bash_profile of the user for which
    >> you want to change the prompt
    >>
    >> export PS1='[\T]$ '


    >Hmm, I tried that, and as I expected, it didn't somehow make the
    >time update "without pressing enter".



    None of the respondants get what you are asking. What you are asking
    for can't be done.

    You have to think of the Unix CLI in terms of that it was designed for TTY's.
    Print a line on the paper, read a line the user types in.

    It doesn't have provisions for updating the data display continously.

    Not that some new program couldn't be written to do that, but in general,
    the shells are still stuck in the day and age of TTYs. Hard copy print.


  7. Re: To Change PS1 automatically

    On Oct 31, 3:04 am, lak wrote:
    > I want to change PS1 prompt to my system time.
    > For that I wrote PS1='\T'. But This will get effect only when i press
    > enter.
    > I want to update PS1 to current time without pressing enter?
    > How can i do this?


    I guess you can get the source code of any shell and modify the code
    to do what you want. You can add a timeout while the shell is waiting
    for input. After time out, you print \r with new system time. If the
    shell input is in line mode, then this will affect your input if you
    are typing something . If the shell input is in character mode, then
    you can stop the timeout when the first character is received, and
    just wait for more input with \n at the end. But, that will not tell
    you when the command line was completed and you will handle the input
    characters (left arrow, right arrow, del, backspace, etc.)

    I use tcsh, and the command history will show the time for each
    command. But, it does not show date (maybe it is configurable). Is
    that what you want?

    Hsing


  8. Re: To Change PS1 automatically

    2007-10-31, 03:04(-07), lak:
    > I want to change PS1 prompt to my system time.
    > For that I wrote PS1='\T'. But This will get effect only when i press
    > enter.
    > I want to update PS1 to current time without pressing enter?
    > How can i do this?


    You may be able to get something close enough in zsh with:

    RPS1=%D{%H:%M:%S}'
    trap 'zle -I; echoti cuu1' ALRM
    TMOUT=1

    Personnaly, I use GNU screen, with which I can have the time in
    a status bar or in the window title. You can also tell your
    shell to update part of screen's status bar.

    --
    Stéphane

  9. Re: To Change PS1 automatically

    Doug McIntyre wrote:
    > Logan Shaw writes:
    >> meenakshi wrote:
    >>> On Oct 31, 3:04 pm, lak wrote:
    >>>> I want to change PS1 prompt to my system time.
    >>>> For that I wrote PS1='\T'. But This will get effect only when i press
    >>>> enter.
    >>>> I want to update PS1 to current time without pressing enter?
    >>>> How can i do this?
    >>> Give the following entry in $HOME/.bash_profile of the user for which
    >>> you want to change the prompt
    >>>
    >>> export PS1='[\T]$ '

    >
    >> Hmm, I tried that, and as I expected, it didn't somehow make the
    >> time update "without pressing enter".


    > None of the respondants get what you are asking. What you are asking
    > for can't be done.


    I think you mean none of the respondents get what meenakshi was asking,
    not what I was asking. I wasn't the person asking the question; I just
    was pointing out that the person answering it didn't give an answer
    that works.

    - Logan

  10. Re: To Change PS1 automatically

    2007-11-01, 20:36(+00), Stephane CHAZELAS:
    > 2007-10-31, 03:04(-07), lak:
    >> I want to change PS1 prompt to my system time.
    >> For that I wrote PS1='\T'. But This will get effect only when i press
    >> enter.
    >> I want to update PS1 to current time without pressing enter?
    >> How can i do this?

    >
    > You may be able to get something close enough in zsh with:
    >
    > RPS1=%D{%H:%M:%S}'
    > trap 'zle -I; echoti cuu1' ALRM
    > TMOUT=1


    Better:

    PS1="%{$terminfo[sc]%}$PS1"
    PS2="%{$terminfo[sc]%}$PS2"
    PS3="%{$terminfo[sc]%}$PS3"
    RPS1='%D{%H:%M:%S}'
    trap 'zle -I; echoti rc' ALRM
    TMOUT=1

    >
    > Personnaly, I use GNU screen, with which I can have the time in
    > a status bar or in the window title. You can also tell your
    > shell to update part of screen's status bar.


    --
    Stéphane

  11. Re: To Change PS1 automatically

    2007-11-01, 16:03(+00), Doug McIntyre:
    [...]
    > None of the respondants get what you are asking. What you are asking
    > for can't be done.
    >
    > You have to think of the Unix CLI in terms of that it was designed for TTY's.
    > Print a line on the paper, read a line the user types in.
    >
    > It doesn't have provisions for updating the data display continously.
    >
    > Not that some new program couldn't be written to do that, but in general,
    > the shells are still stuck in the day and age of TTYs. Hard copy print.


    I can't see why not. Fish does syntax higlighting on the fly,
    zsh provides with a completion system where completions are
    being displayed in colors (à la GNU ls) and selectable with the
    cursor keys. A curses module for zsh is currently being
    developped which promisses some nice future zsh features.

    Most modern terminal emulators have enhanced capabilities
    including a wide range of colors (most support 256 now), mouse
    support, font switching... A shell (when interactive) being a
    terminal application like any other, I can't see why it couldn't
    use those capabilities.

    --
    Stéphane

  12. Re: To Change PS1 automatically

    On Fri, 02 Nov 2007 09:13:28 GMT, Stephane CHAZELAS
    wrote:

    >2007-11-01, 16:03(+00), Doug McIntyre:
    >[...]
    >> None of the respondants get what you are asking. What you are asking
    >> for can't be done.
    >>
    >> You have to think of the Unix CLI in terms of that it was designed for TTY's.
    >> Print a line on the paper, read a line the user types in.
    >>
    >> It doesn't have provisions for updating the data display continously.
    >>
    >> Not that some new program couldn't be written to do that, but in general,
    >> the shells are still stuck in the day and age of TTYs. Hard copy print.

    >
    >I can't see why not. Fish does syntax higlighting on the fly,
    >zsh provides with a completion system where completions are
    >being displayed in colors (à la GNU ls) and selectable with the
    >cursor keys. A curses module for zsh is currently being
    >developped which promisses some nice future zsh features.
    >
    >Most modern terminal emulators have enhanced capabilities
    >including a wide range of colors (most support 256 now), mouse
    >support, font switching... A shell (when interactive) being a
    >terminal application like any other, I can't see why it couldn't
    >use those capabilities.


    The fact that the shell is capable of displaying colours and so on is
    irrelevant. The point Doug was making was that when the shell is
    waiting for input, it is just _waiting_. There is no 'background'
    thread running which can update the display while the main thread is
    waiting.
    --
    PGP key ID 0xEB7180EC

  13. Re: To Change PS1 automatically

    2007-11-02, 09:31(+00), Keith Willis:
    [...]
    > The fact that the shell is capable of displaying colours and so on is
    > irrelevant. The point Doug was making was that when the shell is
    > waiting for input, it is just _waiting_. There is no 'background'
    > thread running which can update the display while the main thread is
    > waiting.


    There's nothing preventing the shell to have a background thread,
    or an alarm(2) ready to kick in. At least zsh has support for
    scheduled jobs.

    bash, ksh and zsh are able to arm alarms (via $TMOUT), but only
    zsh apparently is able to make use of them as I demonstrated in
    another post.

    zsh also has the ability to watch additional file descriptors
    and to act when there's input comming in. So, it can react on
    external input while it's sitting waiting for use input. See zle
    -F. That's how I implemented mouse support for zsh with gpm on
    the Linux console for instance
    (http://stchaz.free.fr/mouse.zsh).

    You can do things like

    exec 6< <(cmd that generates events)
    zle -F 6 zsh-function-that-handles-those-events

    For instance.

    --
    Stéphane

  14. Re: To Change PS1 automatically

    On Nov 1, 5:03 pm, Doug McIntyre wrote:
    > Logan Shaw writes:
    > >meenakshi wrote:
    > >> On Oct 31, 3:04 pm, lak wrote:
    > >>> I want to change PS1 prompt to my system time.
    > >>> For that I wrote PS1='\T'. But This will get effect only when i press
    > >>> enter.
    > >>> I want to update PS1 to current time without pressing enter?
    > >>> How can i do this?

    >
    > >> Give the following entry in $HOME/.bash_profile of the user for which
    > >> you want to change the prompt

    >
    > >> export PS1='[\T]$ '

    > >Hmm, I tried that, and as I expected, it didn't somehow make the
    > >time update "without pressing enter".

    >
    > None of the respondants get what you are asking. What you are asking
    > for can't be done.
    >
    > You have to think of the Unix CLI in terms of that it was designed for TTY's.
    > Print a line on the paper, read a line the user types in.
    >
    > It doesn't have provisions for updating the data display continously.
    >
    > Not that some new program couldn't be written to do that, but in general,
    > the shells are still stuck in the day and age of TTYs. Hard copy print.


    Maybe if you read carefully, you would have seen that I've already
    pointed to the same fact (above).

    As for reprogramming the shell, why not, yes, you can even program it
    yourself and make it your shell, but it won't be bash nor any of the
    currently available shells. But in bash, no, you can't do that.


  15. Re: To Change PS1 automatically

    Stephane CHAZELAS wrote:
    > There's nothing preventing the shell to have a background thread,
    > or an alarm(2) ready to kick in. At least zsh has support for
    > scheduled jobs.
    >
    > bash, ksh and zsh are able to arm alarms (via $TMOUT), but only
    > zsh apparently is able to make use of them as I demonstrated in
    > another post.


    bash (and ksh, I think) can also respond asynchronously to other types
    of events. For instance, witness the effects of "set -o notify".

    Also, I've never tried using select() (or equivalents like poll()) on
    a terminal, but it seems like a shell might be able to use select()
    with a timeout if it wanted to display a clock.

    Having said all that, the historical context of Unix is that there
    are actually 3 environments in which the shell might execute:
    (1) non-interactive
    (2) teletype (cursor cannot move around screen randomly)
    (3) interactive terminal

    There's no reason a shell couldn't theoretically update the time
    every second on a teletype, but it would probably be a waste of paper
    and ink if it did. :-)

    - Logan

  16. Re: To Change PS1 automatically

    On Nov 3, 9:18 am, Logan Shaw wrote:
    > Stephane CHAZELAS wrote:
    > > There's nothing preventing the shell to have a background thread,
    > > or an alarm(2) ready to kick in. At least zsh has support for
    > > scheduled jobs.

    >
    > > bash, ksh and zsh are able to arm alarms (via $TMOUT), but only
    > > zsh apparently is able to make use of them as I demonstrated in
    > > another post.

    >
    > bash (and ksh, I think) can also respond asynchronously to other types
    > of events. For instance, witness the effects of "set -o notify".
    >
    > Also, I've never tried using select() (or equivalents like poll()) on
    > a terminal, but it seems like a shell might be able to use select()
    > with a timeout if it wanted to display a clock.
    >
    > Having said all that, the historical context of Unix is that there
    > are actually 3 environments in which the shell might execute:
    > (1) non-interactive
    > (2) teletype (cursor cannot move around screen randomly)
    > (3) interactive terminal
    >
    > There's no reason a shell couldn't theoreticallyupdatethe time
    > every second on a teletype, but it would probably be a waste of paper
    > and ink if it did. :-)
    >
    > - Logan


    OK thank you friends for replying to me.


  17. Re: To Change PS1 automatically

    2007-11-02, 23:18(-05), Logan Shaw:
    > Stephane CHAZELAS wrote:
    >> There's nothing preventing the shell to have a background thread,
    >> or an alarm(2) ready to kick in. At least zsh has support for
    >> scheduled jobs.
    >>
    >> bash, ksh and zsh are able to arm alarms (via $TMOUT), but only
    >> zsh apparently is able to make use of them as I demonstrated in
    >> another post.

    >
    > bash (and ksh, I think) can also respond asynchronously to other types
    > of events. For instance, witness the effects of "set -o notify".


    ITYM "synchronously" above. Every POSIX shell supports that.

    Here, they respond to the SIGCHLD signal.

    You could do things like:
    trap '((sleep 1; kill -s USR1 "$$") &)' USR1
    kill -s USR1 "$$"

    to have something done about every second. However, with those
    shells, I don't think there's a command to redisplay the prompt.

    > Also, I've never tried using select() (or equivalents like poll()) on
    > a terminal, but it seems like a shell might be able to use select()
    > with a timeout if it wanted to display a clock.


    That's what zsh does when you install a handler for a specific
    fd with zsh -F.

    > Having said all that, the historical context of Unix is that there
    > are actually 3 environments in which the shell might execute:
    > (1) non-interactive
    > (2) teletype (cursor cannot move around screen randomly)
    > (3) interactive terminal
    >
    > There's no reason a shell couldn't theoretically update the time
    > every second on a teletype, but it would probably be a waste of paper
    > and ink if it did. :-)

    [...]

    But $TERM will tell the shell what type of terminal it is
    dealing with and it is already adapting its behavior to it.

    --
    Stéphane

+ Reply to Thread