Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I created a script that sets up a bash prompt. Currently I run it from each user's .bashrc file. I would like to have it run universally (each time any user logs in) as if run from that user's .bashrc.
I looked at /etc/profile. It appears that only gets processed when logged in as root.
So, where would I put such a command?
P.S. Please don't say rc.local, as this is run BEFORE login.
Why not in the global bashrc? For example on OpenSuSE it is /etc/bash.bashrc, on Fedora /etc/bashrc and so on. This is usually the place where PS1 is set. You can change it at your pleasure.
Why not in the global bashrc? For example on OpenSuSE it is /etc/bash.bashrc, on Fedora /etc/bashrc and so on. This is usually the place where PS1 is set. You can change it at your pleasure.
Yeah, so I guess I figured that one out at about the same time you were replying to me. Heh. But now I'm a bit more confused.
I copied the script I originally had in my .bashrc into a new file and gave the file execute permission. Now when I log in I get the junk default prompt, which is expected as I removed the code from .bashrc.
Now, I've got the same code in this executable file, but when I manually run it it does not reset my prompt. Note, that I recopied the script I placed in the executable back into my .bashrc and it works as before. So I know there's not a problem with the script.
Here's the code and execute status of the file:
Code:
$ cat SetCommandPrompt
PROMPTCOLOR='\[\e[1;37m\]'
COMMANDCOLOR='\[\e[0;37m\]'
ROOTCOLOR='\[\e[1;31m\]'
NORMALUSERCOLOR='\[\e[1;32m\]'
HOSTNAMECOLOR='\[\e[1;35m\]'
DIRECTORYCOLOR='\[\e[1;33m\]'
### username ###
if [ `whoami` = "root" ]
then USERCOLOR=$ROOTCOLOR ; PROMPTCHAR="#"
else USERCOLOR=$NORMALUSERCOLOR ; PROMPTCHAR="\$"
fi
PS1=$USERCOLOR"\u"
### hostname ###
#PS1="$PS1$PROMPTCOLOR@"
#PS1="$PS1$HOSTNAMECOLOR$(hostname -s)"
### working directory ###
PS1="$PS1$PROMPTCOLOR ["
PS1="$PS1$DIRECTORYCOLOR\w"
PS1="$PS1$PROMPTCOLOR]"
### prompt end marker ###
PS1="$PS1$USERCOLOR $PROMPTCHAR "
### command color ###
PS1="$PS1$COMMANDCOLOR"
### export ###
export PS1
Ok, so copying the contents of my script into /etc/bashrc allows it to work. This is not optimal for me, as I would like to be able to call the script from bashrc, not copy it. Almost like I can't set PS1 in anything but a bashrc file?
Leads me to another question. How does bash execute /etc/bashrc and ~/.bashrc if they are not executable?
You have to source it, not execute it. If you execute it, the variable PS1 is set only within the scope of the script itself. Therefore in the /etc/bashrc you can put
Code:
. SetCommandPrompt
where the dot in bash stats for the source command. For this reason the x permission is not necessary since the script is not actually executed, but simply "included" in the current shell.
Edit: I checked and in bash you can also use the command source, which is equivalent to the dot. I used the dot in the Bourne Shell /bin/sh where the command source was not available.
You have to source it, not execute it. If you execute it, the variable PS1 is set only within the scope of the script itself. Therefore in the /etc/bashrc you can put
Code:
. SetCommandPrompt
where the dot in bash stats for the source command. For this reason the x permission is not necessary since the script is not actually executed, but simply "included" in the current shell.
Edit: I checked and in bash you can also use the command source, which is equivalent to the dot. I used the dot in the Bourne Shell /bin/sh where the command source was not available.
That worked. Love you!
What is the difference between executing and sourcing? In this way, it's like I included my script as part of the bashrc script?
Also, I thought the export command in "export PS1" is supposed to export the PS1 variable to the shell, so that it isn't only available inside the script?
What is the difference between executing and sourcing? In this way, it's like I included my script as part of the bashrc script?
Exactly!
Quote:
Also, I thought the export command in "export PS1" is supposed to export the PS1 variable to the shell, so that it isn't only available inside the script?
No. The export command inside a script makes a variable available to all the subsequent commands (this is useful when you execute a script from inside another script). In other words it creates the environment for the child processes, but does not change the environment back to the parent.
If you refer to the export command inside the initialization files, like /etc/bashrc or /etc/profile, remember that they are sourced from the shell. This is equivalent to run export from the parent shell.
I'm learning here. Figuring out bash scripting, and you're helping me a lot. Thanks.
You're welcome!
Quote:
So, for clarification ...
- Sourcing a file is effectively the same as placing a copy of the file inside your current script. Like including a function?
Yes. From the bash builtins man page: source: read and execute commands from filename in the current shell environment. That is, instead of writing commands inside the script, they are written in an extrenal file. Exactly like including a function!
Quote:
- There is not a way in bash to replace the value of an environment variable with one generated in a script?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.