Question

I'd like to move my bash prompt's construction into a function that can build it up modularly. The problem is that I cannot figure out how to get the function's result to be interpreted.

Example:

function build_prompt {
  echo "\@"
}
export PS1="\$(build_prompt)"

My prompt always shows as \@, but should be the current time.

Sure there are ways around this particular example, but I'd like a general solution so I can use it for other escaped components, such as colours.

Was it helpful?

Solution

This is one use case for the PROMPT_COMMAND variable: running a function just before displaying the prompt that updates the value of PS1.

function build_prompt {
    PS1='\@'
}
PROMPT_COMMAND='build_prompt'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top