I have trouble understanding:

  • How is .bashrc parsed
  • How are bash functions constructed

I try to customize my terminal prompt. My .bashrc make use of PROMPT_COMMAND like this (it's actually much more complex):

function prompt_command {
    PS1="-> "


I don't like this solution because it clutters my shell namespace with prompt_command, while it should be hidden from outside of this file (but I can't use unset as that would break the prompt).

So, I figured out I could also set the PROMPT_COMMAND in plain text like this:


As I said, my prompt command is actually much more complex. So, I'm worried about the efficiency of this alternative.

Although the two methods are functionally equivalent, is there a fundamental difference in the way my .bashrc will be parsed and the prompt command will be constructed? Is Bash able to effectively "compile" and "cache" the prompt_command as a function object, or is it parsed for each new shell line the same way as if it would be a string?

  • I do the same thing: my function name is __bash_prompt and I live with it in the namespace. – glenn jackman May 1 at 13:35

The .bashrc file is parsed once each time a new terminal session is started.

During parsing, the function prompt_command is added to the environment variable, but there is no "compilation" step nor "object" constructed as Bash is a single-pass interpreter language.

When a new line is typed in my terminal, the PROMPT_COMMAND is executed, which effectively retrieve prompt_command from the environment variables (the same way I could type prompt_command in my shell) and execute it line by line. Consequently, the behavior is similar to directly assigning the command to PROMPT_COMMAND. Direct assignation should even be (imperceptibly) faster, as it avoid needing to fetch an environment variable.

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.