12510198,

A person in this thread already recommended having different colors for different conditions like ssh and running as root, I havent seen anyone mention this specifically but you can determine if the current working directory is writable with something like [ -w “$(pwd)” ] and set the color to red or print a symbol if it doesnt return true.

Also I recommend putting all the code and logic for your shell prompt in a shell function, and using a substitution shell to put it into the PS1 variable like this:


<span style="color:#323232;">__shellprompt ()
</span><span style="color:#323232;">{
</span><span style="color:#323232;">	if [ "$(id -u)" = 0 ]; then
</span><span style="color:#323232;">		local PROMPT_EMBLEM='#'
</span><span style="color:#323232;">	else
</span><span style="color:#323232;">		local PROMPT_EMBLEM='$'
</span><span style="color:#323232;">	fi
</span><span style="color:#323232;">	printf "%s" "$(whoami)@$(uname -n):$(pwd)"
</span><span style="color:#323232;">	printf "n%c " "$PROMPT_EMBLEM"
</span><span style="color:#323232;">}
</span><span style="color:#323232;">PS1='$(__shellprompt)'
</span>

Now this is just a really barebones example, there is a whole lot more you can do like passing in the last exit code through the argv of your shellprompt function like this PS1=‘$(__shellprompt $?)’ and like print it out if its non-zero so you wont have to like echo $? to see if the last command failed, but you should be able to still do this. In my testing, running the shell prompt function in the subsitiution shell didnt effect the $? variable.

In my first comment on another thread about shell prompts, I posted my full shellprompt, it is slightly outdated (I just changed hostname to uname -n), if you cant find it feel free to send a message or just ask, and I will send you the code.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • linux@lemmy.ml
  • localhost
  • All magazines
  • Loading…
    Loading the web debug toolbar…
    Attempt #