Wednesday, May 15, 2024
13
rated 0 times [  13] [ 0]  / answers: 1 / hits: 41722  / 3 Years ago, thu, september 23, 2021, 10:08:30

Before you click away, this isn't the typical "how do I make my bash prompt have color" question. I've already customized my bash prompt to look like this:



[user @ host]----[$(pwd)]
$


where everything in brackets is light blue, and everything else (including $) is black by adding the following to my ~/.bashrc file



# Turn the prompt symbol red if the user is root
if [ $(id -u) -eq 0 ];
then # you are root, make the prompt red
PS1="[e[01;34mu @ he[00m]----[e[01;34m$(pwd)e[00m]
e[01;31m#e[00m "
else
PS1="[e[01;34mu @ he[00m]----[e[01;34m$(pwd)e[00m]
$ "
fi


The goal is to make it such that the only thing that changes when I use 'sudo su' is that the black $ changes into a red #. I've looked in /etc/bash.bashrc and /etc/profile to see if there is just a line to comment out, but there is a bunch of stuff about debian_chroot that I don't understand, and I don't want to screw something up. How can I accomplish what I want?



P.S. This is what I want the prompt to look like as root



[user @ host]----[$(pwd)]
(red)#


edit: Mark this solved, appending the above code to ~/.bashrc while root accomplished my goal. Also, in the above code, $(pwd) only displays the home directory (I guess because that is the working directory when the terminal is opened), and never updates. Replacing $(pwd) with w fixes this, but displays the home directory as ~, which I was trying to avoid.


More From » command-line

 Answers
3

It will also depend on how you become the root user. You need to make the change in the root user's .bashrc if you are using something like su - root or sudo -i, where you read in the environment.



With sudo -s, you should be reading your own .bashrc.



Consider adding some printf or echo statements to debug your code, to tell you when it has executed.



Use the id command to make sure you are who you think you are:



root@tau:~# id
uid=0(root) gid=0(root) groups=0(root)

[#30839] Friday, September 24, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ipentainer

Total Points: 112
Total Questions: 113
Total Answers: 113

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;