Recently I stumbled over an article, about how to customize your shell prompt. What really surprised me, is that it lacked one of the most basic tips I learned nearly 20 years back: Always display a timestamp in the prompt, to be able to check how long a process is running or when it ended. (Don’t need it daily, but every so often it saves my butt. ;-)) The other trick is to always have a colorful prompt, to easily discern where output from programs start/stop. In total my PS1 looks like this (with GIT status at the end): [\e[32m]\u[\e[m]@[\e[35m]\h[\e[m] [\e[36m]\A[\e[m] [\e[37m][[\e[m][\e[31m]\w[\e[m][\e[37m]][\e[m]$(__git_ps1 “(%s)”)$

My question is, what customization, tips and tricks do you have for the shell prompt?

  • 12510198@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    1
    ·
    10 months ago

    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:

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

    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.

  • vojel@feddit.de
    link
    fedilink
    arrow-up
    0
    ·
    10 months ago

    I got different colors for Kubernetes clusters. Like green for testing cluster, yellow for development and red for production. Always taking a Quick Look before I do something