How to generate PS1 in a function (escaping issues)

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.

Answers


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'

Need Your Help

From Design to Development: Is there a common EmberJS workflow?

design ember.js workflow ember-app-kit ember-cli

This question is subjective by nature, but I am curious about a specific thing, so hopefully there is a decent answer.

How to get selected radio button data in AngularJS (ng-repeat)

javascript angularjs

I need to get selected row data(id, name) with radio button. To be specific;

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.