Live data from Hacker News

Bash PS1 Generator

bashrcgenerator.com

131–140 of 146 posts

Re: Bash PS1 Generator

#131

Earlier quoted context omitted.

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.

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

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

Re: Bash PS1 Generator

#133
post #68
post #46

Am I the only one who leaves mine as is?

Well, the default prompt you have may already have been customized by operating system vendor (Linux distro) and not be the real bash default. Try this to get the real default: PS1='[\u@\h \W]\$ '

PS1='$ '

or

PS!='# '

Re: Bash PS1 Generator

#134
post #46

Am I the only one who leaves mine as is?

I leave the one on remote hosts the same. I work mostly on a terminal in my laptop locally and mostly just have the time and the command number listed so I can see things in the scrollback.

Re: Bash PS1 Generator

#135

On the site all prompt variables end in "tput sgr0". I haven't seen that before. What does that do and why is it needed?

Rather than turn off all attributes with "tput sgr0", instead I Save Cursor with "tput sc" at the beginning to save attributes, and Restore Cursor with "tput rc" at the end to restore attributes.

Save and Restore cursor doesn't always work because I don't necessarily need the vertical position restored, for example:

  ls_color () { 
    tput sc;
    /bin/ls --color=auto "$@";
    push_cursor_position;
    tput rc;
    pop_cursor_position
  }

Re: Bash PS1 Generator

#136

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…

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

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

Re: Bash PS1 Generator

#137
My customized PS1 only contains the current path.

Additionally, I have used git-prompt as PROMPT_COMMAND for a long time, and it's certainly the most useful piece of information.

Re: Bash PS1 Generator

#138

I've been using this for years (output of 'echo $PS1' ): [\e[1;33m\u\e[1;32m@\e[1;31m\H \e[1;36m\t\e[0m] [\e[1;35m\w\e[0m](\e[1;32m\#\e[0m )\n$ which presents as: [User@HOST 10:21:07][~](2) $ (Username, hostname, 24 hour local time, CWD, history count, newline, prompt) More detail from my .bashrc: # Define some colors first: red='\e[0;31m' RED='\e[1;31m' blue='\e[0;34m' BLUE='\e[1;34m' cyan='\e[0;36m' CYAN='\e[1;36m'…

You need to add some \[ and \] around the escape codes to tell the shell they’re non-printing characters. Otherwise, things like line-wrapping don’t work right. It’s not a problem because of your \n makes it irrelevant, but just a heads up for the future.

Re: Bash PS1 Generator

#139

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 --de…

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.

Re: Bash PS1 Generator

#140

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 --de…

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.

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.
Post reply on HN