LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 02-04-2015, 03:10 PM   #1
whim
LQ Newbie
 
Registered: Oct 2014
Distribution: CentOS
Posts: 16

Rep: Reputation: Disabled
Help with the modifying apects of .bash_profile


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"?
 
Old 02-04-2015, 03:15 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
The best place to start is with the bash documentation --> https://www.gnu.org/software/bash/ma...l#SEC_Contents Section 6.2 discusses the startup files.

Here is a good place to start looking into the bash prompt --> http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/
 
Old 02-04-2015, 09:57 PM   #3
whim
LQ Newbie
 
Registered: Oct 2014
Distribution: CentOS
Posts: 16

Original Poster
Rep: Reputation: Disabled
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?
 
Old 02-04-2015, 10:11 PM   #4
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
Quote:
Originally Posted by whim View Post
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.
 
Old 02-04-2015, 10:52 PM   #5
whim
LQ Newbie
 
Registered: Oct 2014
Distribution: CentOS
Posts: 16

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-04-2015, 11:13 PM   #6
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
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.

Last edited by Miati; 02-04-2015 at 11:19 PM.
 
Old 02-04-2015, 11:28 PM   #7
whim
LQ Newbie
 
Registered: Oct 2014
Distribution: CentOS
Posts: 16

Original Poster
Rep: Reputation: Disabled
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.
 
Old 02-04-2015, 11:38 PM   #8
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
Code:
[-f: command not found
Make sure it is like this
Code:
if [ -f ~/.bash_profile ]; then source ~/.bash_profile; fi
Not like this
Code:
if [-f ~/.bash_profile ]; then source ~/.bash_profile; fi
Notice the space.
 
Old 02-05-2015, 12:03 AM   #9
whim
LQ Newbie
 
Registered: Oct 2014
Distribution: CentOS
Posts: 16

Original Poster
Rep: Reputation: Disabled
I fixed it, but now the terminal just opens with only the cursor, no prompt. When I enter a command (like cd) it crashes.
 
Old 02-05-2015, 03:15 AM   #10
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by whim View Post
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.

Like this:

Code:
export PS1="#\! \u@\h in \w > "
 
Old 02-05-2015, 03:18 AM   #11
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by Miati View Post
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)
~/.bashrc does not loads when terminal is started.

Instead bash loads /etc/profile then only .bash_profile ( if not then .bash_login or .profile ).
 
Old 02-05-2015, 05:07 AM   #12
whim
LQ Newbie
 
Registered: Oct 2014
Distribution: CentOS
Posts: 16

Original Poster
Rep: Reputation: Disabled
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.

Last edited by whim; 02-05-2015 at 05:39 AM.
 
Old 02-05-2015, 08:03 AM   #13
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
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.
 
Old 02-05-2015, 09:01 AM   #14
whim
LQ Newbie
 
Registered: Oct 2014
Distribution: CentOS
Posts: 16

Original Poster
Rep: Reputation: Disabled
My current .bash_profile is

Code:
# 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?

Last edited by whim; 02-05-2015 at 09:09 AM.
 
Old 02-05-2015, 09:29 AM   #15
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Questions about modifying bash_profile values, export, etc. sethbw Linux - Newbie 14 04-20-2012 03:11 PM
About .bash_profile ravi_nandula Linux - Newbie 3 04-10-2012 02:29 PM
bash_profile strycnine Slackware 5 10-22-2006 02:11 PM
where is ~/.bash_profile? Jskill007 Slackware 4 09-17-2004 11:04 PM
.bash_profile praveenv Linux - Newbie 3 08-14-2004 12:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration