Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Cool .bashrc
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Gentoo Chat
View previous topic :: View next topic  
Author Message
yaslam
n00b
n00b


Joined: 08 May 2024
Posts: 45

PostPosted: Fri Apr 18, 2025 1:11 pm    Post subject: Cool .bashrc Reply with quote

======== UPDATE: SEE UPDATED .BASHRC AT END OF THREAD ==========

Hi everyone, I am sharing my bashrc for the root user here (requires nerd fonts).

FEATURES:
- Gentoo icon beside username@hostname (nerd font required)
- Git status
- Time the command was executed

PHOTO: https://imgur.com/rn4IxTR


Code:

alias edit="zile"

GRAY="\001\e[2;37m\002"
GREEN="\001\e[0;32m\002"
LIGHTGREEN="\001\e[92m\002"
PURPLE="\001\e[1;35m\002"
BLUE="\001\e[34m\002"
COLOR_NONE="\001\e[0m\002"
RED="\001\e[31m\002"
BOLD="\001\e[1m\002"

function set_bash_prompt () {
    PS1=""
    # set up user and host
    PS1+="${RED}  ${BOLD}${RED}\u@\h${COLOR_NONE} "
    # set up working directory
    PS1+="${GREEN}\w${COLOR_NONE} "
    # set up git branch
    PS1+="${GRAY}${BRANCH}${COLOR_NONE} "
    # set up time
    PS1+="${GRAY}@$(date '+%H:%M:%S')"
    # new line before prompt
    PS1+="\n"
    # set up prompt character
    PS1+="${COLOR_NONE}#${COLOR_NONE}"
   
    PS1="\r${PS1} "
}

export PROMPT_COMMAND=set_bash_prompt




Last edited by yaslam on Fri Apr 18, 2025 3:05 pm; edited 2 times in total
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23356

PostPosted: Fri Apr 18, 2025 1:29 pm    Post subject: Re: Cool .bashrc Reply with quote

yaslam wrote:
FEATURES:
- Git status
Personally, I would not want a root shell running git commands automatically. The git project makes substantial efforts to make this safe even in an untrusted repository, but there is always the possibility of a new problem.
yaslam wrote:
- Time the command was executed
This appears to be the time the prompt was rendered, not the time the child process started.
yaslam wrote:
Code:
function set_bash_prompt () {
    PS1=""
    # set up user and host
    PS1+="${RED}  ${BOLD}${RED}\u@\h${COLOR_NONE} "
Historically, Bash prompts needed special markers to tell Bash which parts of the prompt were zero-width. Without these markers, bash would measure the prompt incorrectly, triggering problems with wrapping and redraw. I am not aware of this requirement being lifted, but I see no markers in your prompt. Therefore, I think this will misbehave.
yaslam wrote:
Code:
    # set up working directory
    PS1+="${GREEN}\w${COLOR_NONE} "
    # set up git branch
    PS1+="${GRAY}${BRANCH}${COLOR_NONE} "
The variable BRANCH is not maintained anywhere in the shown code, nor do I see any calls out to standard functions that would maintain it. I think this will not work for other users who copy the code you shared.
yaslam wrote:
Code:
    # set up time
    PS1+="${GRAY}@$(date '+%H:%M:%S')"
Bash can generate the current time without shelling out to date.
yaslam wrote:
Code:

    # new line before prompt
    PS1+="\n"
    # set up prompt character
    PS1+="${COLOR_NONE}#${COLOR_NONE}"
This is redundant. You set the color to NONE twice without any intervening changes.

As a general comment, resetting the color only to display a space and then change the color again seems wasteful. I suggest only resetting the color when it will be visible, and then once at the end so that the user's text is the default color.
Back to top
View user's profile Send private message
sMueggli
Guru
Guru


Joined: 03 Sep 2022
Posts: 579

PostPosted: Fri Apr 18, 2025 1:49 pm    Post subject: Reply with quote

What commands and work do you execute as user root that is worth the effort to modify the shell configuration?
Back to top
View user's profile Send private message
yaslam
n00b
n00b


Joined: 08 May 2024
Posts: 45

PostPosted: Fri Apr 18, 2025 1:50 pm    Post subject: Re: Cool .bashrc Reply with quote

Hu wrote:
yaslam wrote:
FEATURES:
- Git status
Personally, I would not want a root shell running git commands automatically. The git project makes substantial efforts to make this safe even in an untrusted repository, but there is always the possibility of a new problem.
yaslam wrote:
- Time the command was executed
This appears to be the time the prompt was rendered, not the time the child process started.
yaslam wrote:
Code:
function set_bash_prompt () {
    PS1=""
    # set up user and host
    PS1+="${RED}  ${BOLD}${RED}\u@\h${COLOR_NONE} "
Historically, Bash prompts needed special markers to tell Bash which parts of the prompt were zero-width. Without these markers, bash would measure the prompt incorrectly, triggering problems with wrapping and redraw. I am not aware of this requirement being lifted, but I see no markers in your prompt. Therefore, I think this will misbehave.
yaslam wrote:
Code:
    # set up working directory
    PS1+="${GREEN}\w${COLOR_NONE} "
    # set up git branch
    PS1+="${GRAY}${BRANCH}${COLOR_NONE} "
The variable BRANCH is not maintained anywhere in the shown code, nor do I see any calls out to standard functions that would maintain it. I think this will not work for other users who copy the code you shared.
yaslam wrote:
Code:
    # set up time
    PS1+="${GRAY}@$(date '+%H:%M:%S')"
Bash can generate the current time without shelling out to date.
yaslam wrote:
Code:

    # new line before prompt
    PS1+="\n"
    # set up prompt character
    PS1+="${COLOR_NONE}#${COLOR_NONE}"
This is redundant. You set the color to NONE twice without any intervening changes.

As a general comment, resetting the color only to display a space and then change the color again seems wasteful. I suggest only resetting the color when it will be visible, and then once at the end so that the user's text is the default color.


Hu, thanks for your analysis. I will try to implement your improvements.

Regards,
Yusef Aslam
Back to top
View user's profile Send private message
yaslam
n00b
n00b


Joined: 08 May 2024
Posts: 45

PostPosted: Fri Apr 18, 2025 1:51 pm    Post subject: Reply with quote

sMueggli wrote:
What commands and work do you execute as user root that is worth the effort to modify the shell configuration?


Just your usual commands. But I like having prompts that look nice, that's the only reason lol :) .
Back to top
View user's profile Send private message
yaslam
n00b
n00b


Joined: 08 May 2024
Posts: 45

PostPosted: Fri Apr 18, 2025 2:19 pm    Post subject: .bashrc v2 Reply with quote

I have implemented suggestions made by Hu and also removed a few features as they are too complex to implement:
- Git status was not implemented
- Command time was not implemented (shows time prompt was rendered)
- Removed redundant ${COLOR_NONE}
- Get time using \t instead of date
- Added zero-width characters to PS1 variable at the start.

Code:

GRAY="\001\e[2;37m\002"
GREEN="\001\e[0;32m\002"
LIGHTGREEN="\001\e[92m\002"
PURPLE="\001\e[1;35m\002"
BLUE="\001\e[34m\002"
COLOR_NONE="\001\e[0m\002"
RED="\001\e[31m\002"
BOLD="\001\e[1m\002"

function set_bash_prompt () {
    PS1="\[\]"
    # set up user and host
    PS1+="${COLOR_NONE}${RED}  ${BOLD}${RED}\u@\h "
    # set up working directory
    PS1+="${GREEN}\w "
    # set up time
    PS1+="${COLOR_NONE}${GRAY}@\t"
    # new line before prompt
    PS1+="\n"
    # set up prompt character
    PS1+="#${COLOR_NONE}"

    PS1="\r${PS1} "
}

export PROMPT_COMMAND=set_bash_prompt



Suggestions are welcome.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23356

PostPosted: Fri Apr 18, 2025 2:30 pm    Post subject: Reply with quote

Zero width markers need to surround the characters that are zero-width, rather than being placed at the start of the string. So you would write PS1="\[${COLOR_RED}\]some-visible-red-text\[${COLOR_NONE}\]" and so on.
Back to top
View user's profile Send private message
yaslam
n00b
n00b


Joined: 08 May 2024
Posts: 45

PostPosted: Fri Apr 18, 2025 2:45 pm    Post subject: .bashrc v3 Reply with quote

Hu wrote:
Zero width markers need to surround the characters that are zero-width, rather than being placed at the start of the string. So you would write PS1="\[${COLOR_RED}\]some-visible-red-text\[${COLOR_NONE}\]" and so on.


I see thank you!

.bashrc v3:
CHANGES:
- Add zero-width markers to all variables that modify the text color etc..
- Rename some variables for clarity.

Code:

COLOR_GRAY="\001\e[2;37m\002"
COLOR_GREEN="\001\e[0;32m\002"
COLOR_LIGHTGREEN="\001\e[92m\002"
COLOR_PURPLE="\001\e[1;35m\002"
COLOR_BLUE="\001\e[34m\002"
COLOR_RED="\001\e[31m\002"
BOLD="\001\e[1m\002"
RESET_COLOR="\001\e[0m\002"

function set_bash_prompt () {
    PS1=""
    # set up user and host
    PS1+="\[${RESET_COLOR}\]\[${COLOR_RED}\]  \[${BOLD}\]\[${COLOR_RED}\]\u@\h "
    # set up working directory
    PS1+="\[${COLOR_GREEN}\]\w "
    # set up time
    PS1+="\[${RESET_COLOR}\]\[${COLOR_GRAY}\]@\t\[${RESET_COLOR}\]"
    # new line before prompt
    PS1+="\n"
    # set up prompt character
    PS1+="#"

    PS1="\r${PS1} "
}

export PROMPT_COMMAND=set_bash_prompt


Suggestions welcome.
Back to top
View user's profile Send private message
Ralphred
l33t
l33t


Joined: 31 Dec 2013
Posts: 767

PostPosted: Fri Apr 18, 2025 3:06 pm    Post subject: Reply with quote

My ~/.bashrc got too big and messy to deal with, so I use
~/.bashrc:
~~snip~~
# Put your fun stuff here
for file in ~/.bashrc.d/*.conf
do
        source ${file}
done
and
ls ~/.bashrc.d wrote:
00-general.conf
05-remote-aliases.conf
10-sudo-alias.conf
15-functions.conf
20-misc-alias.conf
25-iptables.conf
Back to top
View user's profile Send private message
yaslam
n00b
n00b


Joined: 08 May 2024
Posts: 45

PostPosted: Fri Apr 18, 2025 3:08 pm    Post subject: Reply with quote

Ralphred wrote:
My ~/.bashrc got too big and messy to deal with, so I use
~/.bashrc:
~~snip~~
# Put your fun stuff here
for file in ~/.bashrc.d/*.conf
do
        source ${file}
done
and
ls ~/.bashrc.d wrote:
00-general.conf
05-remote-aliases.conf
10-sudo-alias.conf
15-functions.conf
20-misc-alias.conf
25-iptables.conf



Ralphred, that's a cool idea actually.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Gentoo Chat All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum