[SOLVED] Help with the modifying apects of .bash_profile
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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'm new to Linux and I'm trying to learn about .bash_profile. I want to be able to set up various aspects of it, such as setting default permissions for new files, changing the format of the prompt. I also want to modify it so that the current directory is appended to the default PATH.
Can anybody give me a brief rundown on the code/syntax and whatnot for these things?
ex: what is the line that I would need to type so that my prompt is formatted "command number - user - machine name - current directory"?
How do I get changes made via the terminal to be saved? Apparently, I'm supposed to type "source ~/.bash_profile" or ". ~/.bash_profile" but it doesn't seem to be working.
For example, I typed in PS1="#\! \u@\h in \w > " to modify the prompt, and it worked. I then typed source ~/.bash_profile to save the changes, but when I re-opened the terminal it was back to the default.
Is there something I'm doing wrong? Is there a way to make the changes by editing the .bash_profile in a text editor?
How do I get changes made via the terminal to be saved? Apparently, I'm supposed to type "source ~/.bash_profile" or ". ~/.bash_profile" but it doesn't seem to be working.
For example, I typed in PS1="#\! \u@\h in \w > " to modify the prompt, and it worked. I then typed source ~/.bash_profile to save the changes, but when I re-opened the terminal it was back to the default.
Is there something I'm doing wrong? Is there a way to make the changes by editing the .bash_profile in a text editor?
I think you may misunderstand the purpose of source. source is used to read the file. So if in the file you have the command "a=hello"
then this would ouput hello
Code:
source file
echo $a
The .bash_profile page is read when you open a terminal, so the source read is actually already done.
To modify the file, try typing in terminal:
Code:
gedit ~/.bash_profile
Put in this file
Code:
PS1="#\! \u@\h in \w > "
Then it should load the terminal with the prompt as you set it up.
So I opened up .bash_profile with the gedit command, typed the prompt code in, save it, and restarted the terminal. It still shows only the default prompt (bash-4.1$)
Here's my .bash_profile so far:
Code:
# Get the aliases and functions from local config file:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
#
# Set path here:
#
# Set prompt here:
PS1="#\! \u@\h in \w > "
#
# Set default file permissions here:
# end .bash_profile
Hmm..
You know, I'm not sure if the .bash_profile file is loaded when a terminal is opened. Put this somewhere in your ~/.bashrc (which I know loads)
Code:
if [ -f ~/.bash_profile ]; then source ~/.bash_profile; fi
edit:
According to man page of bash, when bash is loaded on a non-login screen (opening a terminal while logged in for example) it does not read from ~/.bash_profile
Quote:
When an interactive shell that is not a login shell is started, bash
reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if
these files exist. This may be inhibited by using the --norc option.
The --rcfile file option will force bash to read and execute commands
from file instead of /etc/bash.bashrc and ~/.bashrc.
However, it IS loaded on a login screen, so if you logged in with the .bash_profile set, it should load. (Not sure if that applies to gui logins)
Quote:
When bash is invoked as an interactive login shell, or as a non-inter‐
active shell with the --login option, it first reads and executes com‐
mands from the file /etc/profile, if that file exists. After reading
that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
in that order, and reads and executes commands from the first one that
exists and is readable. The --noprofile option may be used when the
shell is started to inhibit this behavior.
I put that code in the .bashrc, saved, logged out, logged back in, and opened the terminal. Here's what it says:
Code:
bash: [-f: command not found
bash-4.1$
in case it helps, here's my current .bashrc (which was given to me to begin with)
Code:
if [ -f ~/.bash_profile ]; then source ~/.bash_profile; fi
# Set alias(es) here:
# I strongly suggest you not modify anything below this comment!
#
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
I should also probably point out that I took my original .bash_profile and .bashrc and moved them to a folder on my desktop, then replaced them in the home directory with these two new files.
So I opened up .bash_profile with the gedit command, typed the prompt code in, save it, and restarted the terminal. It still shows only the default prompt (bash-4.1$)
Here's my .bash_profile so far:
Code:
# Get the aliases and functions from local config file:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
#
# Set path here:
#
# Set prompt here:
PS1="#\! \u@\h in \w > "
#
# Set default file permissions here:
# end .bash_profile
You have to export the PS1 variable. Then only the prompt would change.
exporting the PS1 variable did not work, but I tried putting PS1="#\! \u@\h in \w > " in .bashrc and it worked. Any reason why this is the case?
edit: I also found that if I put PS1="#\! \u@\h in \w > " in .bash_profile instead of .bashrc and open the terminal the prompt is still the default. But when I type in source .bash_profile the prompt changes.
If you can put whole of /etc/profile and /etc/profile.d/files and /home/you/.bash_profile and /home/you/.bashrc plus .profile or .bash_login then it may be possible to figure out.
# Get the aliases and functions from local config file:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
#
# Set path here
PATH=$PATH:./bin
export PATH
#
# Set prompt here:
export PS1='#\! \u@\h in \w > '
#
# Set default file permissions here:
umask 177
my current .bashrc is:
Code:
# Set alias(es) here:
# I strongly suggest you not modify anything below this comment!
#
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# end .bash_profile
I can't find any of the other files you mentioned. Also, I'm trying to append the current working directory to PATH, and then append /bin so long as there is a directory /bin in the home directory. Am I doing that right in .bash_profile?
When launching a Bash interactive shell, it can be a login shell or a non-login shell. When launching a login shell, Bash reads /etc/profile and then the configuration files in the following order: ~/.bash_profile, ~/.bash_login, and ~/.profile. Bash executes the commands in the first of these files that it can found and read. When launching a non-login shell, it looks for ~/.bashrc.
Sourcing ~/.bashrc from ~/.bash_profile and then sourcing ~/.bash_profile from ~/.bashrc is a loop and is likely why Bash crashed when you did that. Typically, the only thing in ~/.bash_profile is
Code:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
so ~/.bashrc is read whether a login or non-login shell is launched.
If you put your PS1 variable in ~/.bashrc, you will get the prompt you want whether using a login or non-login shell (assuming you source ~/.bashrc from ~/.bash_profile). All the other stuff you have in ~/.bash_login you should move to ~/.bashrc for the same reason.
To append a directory named 'bin' that is in your home directory to your PATH, put this in ~/.bashrc
Code:
export PATH=$PATH:~/bin
or
Code:
export PATH=$PATH:$HOME/bin
depending on whether or not the ~ or $HOME is easier for you to read in the future.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.