View previous topic :: View next topic |
Author |
Message |
yaslam n00b

Joined: 08 May 2024 Posts: 45
|
Posted: Fri Apr 18, 2025 1:11 pm Post subject: Cool .bashrc |
|
|
======== 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 |
|
 |
Hu Administrator

Joined: 06 Mar 2007 Posts: 23356
|
Posted: Fri Apr 18, 2025 1:29 pm Post subject: Re: Cool .bashrc |
|
|
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 |
|
 |
sMueggli Guru

Joined: 03 Sep 2022 Posts: 579
|
Posted: Fri Apr 18, 2025 1:49 pm Post subject: |
|
|
What commands and work do you execute as user root that is worth the effort to modify the shell configuration? |
|
Back to top |
|
 |
yaslam n00b

Joined: 08 May 2024 Posts: 45
|
Posted: Fri Apr 18, 2025 1:50 pm Post subject: Re: Cool .bashrc |
|
|
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 |
|
 |
yaslam n00b

Joined: 08 May 2024 Posts: 45
|
Posted: Fri Apr 18, 2025 1:51 pm Post subject: |
|
|
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 |
|
 |
yaslam n00b

Joined: 08 May 2024 Posts: 45
|
Posted: Fri Apr 18, 2025 2:19 pm Post subject: .bashrc v2 |
|
|
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 |
|
 |
Hu Administrator

Joined: 06 Mar 2007 Posts: 23356
|
Posted: Fri Apr 18, 2025 2:30 pm Post subject: |
|
|
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 |
|
 |
yaslam n00b

Joined: 08 May 2024 Posts: 45
|
Posted: Fri Apr 18, 2025 2:45 pm Post subject: .bashrc v3 |
|
|
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 |
|
 |
Ralphred l33t

Joined: 31 Dec 2013 Posts: 767
|
Posted: Fri Apr 18, 2025 3:06 pm Post subject: |
|
|
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 |
|
 |
yaslam n00b

Joined: 08 May 2024 Posts: 45
|
Posted: Fri Apr 18, 2025 3:08 pm Post subject: |
|
|
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 |
|
 |
|