
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…
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:
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.)
; is also a great prompt, without directory machine name or anything.
I just copy paste my session, and rerun most commands.
Maybe >(Unicode 0xFF1/>) would be a good substitute?
Side by side: >>
that seems like asking for trouble
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):
〉> > ≫ 》
Learned about that when a co-worker did that about 20 years ago.
Terminal app feature idea: a hotkey that puts the last command run in the copy buffer
function copy_last_command {
history 1 | sed 's/ [0-9]* //' | xclip -i
}
bind -x '"\C-h":copy_last_command'The Mac equivalent of xclip -i is pbcopy
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.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!
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".
I've run the bash 10k and all I got was this shirt!
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.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.