Bash PS1 Generator

2021-06-249:34323143bashrcgenerator.com

Examples and presets of PS1 prompts Clicking on an example will replace your selection. 1Available elements Drag and drop to your selection. 2Your selection Double-click to change the color and…

Examples and presets of PS1 prompts Clicking on an example will replace your selection.

1Available elements Drag and drop to your selection.

2Your selection Double-click to change the color and boldness.
Drag an element outside to remove it.
remove everything

3Preview of your prompt This is how your prompt will look like.
4Your generated .bashrc PS1 and additional functions Do you need help for setting up your prompt? read the documentation below!

Help To use this for your prompt, it's easy: Type this in your prompt :

nano ~/.bashrc

Then copy the generated code at the end of the file. Save and exit (in nano, it's CTRL + o, CTRL + x).

To see the changes, either:

  • paste the generated code in your prompt like an usual command
  • logout and login again
  • type "bash" to run a new bash session
References for PS1, bash, .bashrc, etc

Read the original article

Comments

  • By drivers99 2021-06-2415:395 reply

    Seeing '>' as an option makes it somewhat relevant to mention here: I'd recommend not ending your prompt with a '>' character (as you'd see in DOS/Windows a lot, which might make it seem like a nice option). If you accidentally copy and paste your prompt with a command after it, (which is easy to do with a couple mouse clicks, or if you forget what's in your clipboard after copying some lines from your terminal,) the first thing after the > sign is the file for a script or executable and you will truncate the file (make it 0 bytes). This can be especially bad if you're root at the time. (That makes # as a root prompt especially a good idea.)

    • By codemac 2021-06-250:18

      ; is also a great prompt, without directory machine name or anything.

      I just copy paste my session, and rerun most commands.

    • By tyingq 2021-06-2417:542 reply

      Maybe >(Unicode 0xFF1/>) would be a good substitute?

      Side by side: >>

      • By TremendousJudge 2021-06-2419:05

        that seems like asking for trouble

      • By wodenokoto 2021-06-258:48

        Japanese has a few brackets that might be useful but less easy to confuse.

        Here’s a few the IME on my phone suggested (the middle ones are the same as yours as far as I can tell):

        〉> > ≫ 》

    • By RobRivera 2021-06-2420:411 reply

      I would have never thought of that. ty for the tip

      • By drivers99 2021-06-2420:47

        Learned about that when a co-worker did that about 20 years ago.

    • By chrissnell 2021-06-2418:081 reply

      Terminal app feature idea: a hotkey that puts the last command run in the copy buffer

      • By dllthomas 2021-06-2418:401 reply

            function copy_last_command {
                history 1 | sed 's/ [0-9]*  //' | xclip -i
            }
            bind -x '"\C-h":copy_last_command'

        • By bobbyi_settv 2021-06-2420:44

          The Mac equivalent of xclip -i is pbcopy

  • By graton 2021-06-2413:331 reply

    Somewhat related to PS1 is PS4 in bash.

    Make your trace output more useful/verbose in your scripts:

      set -x
      export PS4='+ ${BASH_SOURCE:-}:${FUNCNAME[0]:-}:L${LINENO:-}:   '
    
    This will print the filename, function name, line number, and line content during execution.

    • By formerly_proven 2021-06-2415:141 reply

      In case someone wonders, this does retain the repeated "+" to indicate call depth: "The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection." -- this is an important feature when working on large-ish (1-10+k LoC) scripts, which end up having very deep call stacks and a flat trace output would be completely inscrutable.

      • By Xophmeister 2021-06-2417:102 reply

        If you have 10k LoC Bash scripts, then you’ve got bigger problems!

        • By formerly_proven 2021-06-259:42

          Overall it's more like 150k LoC or something like that (split into the classic three layer architecture: "scripts", "library" and "more scripts on top"). It does work though. Much better than you'd expect from hearing "tens of thousands of lines of shell".

        • By jethro_tell 2021-06-2422:28

          I've run the bash 10k and all I got was this shirt!

  • By kstrauser 2021-06-2418:201 reply

    I was moved to experiment with Fish shell one day, and wanted to see how to set my prompt. Answer: you create a function named `fish_prompt` that uses regular shell commands like `echo` (and fish's own `set_color`) to imperatively build the prompt. Want to make a right-hand prompt for something like the current time? Write a function named `fish_right_prompt`. For example, here's mine:

      function fish_right_prompt --description 'Write out the right prompt'
          set_color 666666
          date +'%Y-%m-%d %H:%M:%S'
          set_color normal
      end
    
    I was sold. I'm never, ever going back to futzing around with a bunch of complex $PSx variables when I can just write a function that does the right thing.

    • By dllthomas 2021-06-252:161 reply

      Note that in bash there is the PROMPT_COMMAND variable, which runs before the prompt is printed and which can be used to print things and to set PS1 if you want more dynamism than bash more directly affords.

      • By dllthomas 2021-06-252:24

        For a "right prompt" showing the time in particular, though acknowledging it'll get overwritten if you're writing a long command (which may or many not be what you want), running `printf "%${COLUMNS}s" "$(date)"` from PROMPT_COMMAND does the trick.

HackerNews