1

I just came across the PS1 variable. It is possible to change the prompt by simply changing this variables value, for instance:

PS1="\h"
hostname as Prompt
PS1="\j"
numbers of current jobs as Prompt
PS1="\t"
current time as Prompt
PS!="\j running on \h :"
or more complex string with many substitutions

Now since there is something like a background process that does not occupy the shell, I thought if it was possible to write a script that changes the value of PS1 each 5 seconds once that could be running whole time in the background.

Pseudo-Script:

Repeat

    {
    PS1="\j jobs"
    sleep 5 seconds
    PS1="time: \t"
    sleep 5 seconds
    PS1="Command History: \!"
    sleep 5 seconds
    }

If it is possible, how would you implement it in bash shell script ?

  • 1
    I don't think it is... but just think how irritating that could be? – Sobrique Mar 31 '15 at 18:23
2

None of the common unix shells (bash, [pd]ksh, [t]csh) have their own, internal analogue to cron in order to do automatic background tasks as you wish.

But even if they did, after you've run a command and it has come back to the prompt, that character output to show the prompt is static, not dynamic. If you sat there and watched, it would not cycle through your PS1 alternatives every 5 seconds like you are thinking. You would not see a new prompt until after you hit enter or ran another command.

However, you might be able to get what you are looking for by using GNU screen which has "taskbar" capabilities. Try starting with this.

2

I don't think it is possible, even if you incorporate the above pseudo-code in a shell script, because on each invocation of the shell script, it will start its own sub-shell where this stuff will happen and not where you want it to (your main shell). Even if you start it in the background. (script.sh &)

  • it is not possible to change settings in the shell from the subshell? – Abdul Al Hazred Mar 31 '15 at 19:47
  • 1
    This is the same reason that a sub-process cannot change it's parent process's working directory. The working directory, like environment variables such as PS1, are part of the process's environment which a child process cannot affect. This is the reason that the cd command must be built into the shell as opposed to being a separate executable. – DoxyLover Mar 31 '15 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.