LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with the modifying apects of .bash_profile (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-the-modifying-apects-of-bash_profile-4175533077/)

whim 02-04-2015 03:10 PM

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"?

weibullguy 02-04-2015 03:15 PM

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/

whim 02-04-2015 09:57 PM

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?

Miati 02-04-2015 10:11 PM

Quote:

Originally Posted by whim (Post 5312284)
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.

whim 02-04-2015 10:52 PM

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


Miati 02-04-2015 11:13 PM

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.

whim 02-04-2015 11:28 PM

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.

Miati 02-04-2015 11:38 PM

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.

whim 02-05-2015 12:03 AM

I fixed it, but now the terminal just opens with only the cursor, no prompt. When I enter a command (like cd) it crashes.

veerain 02-05-2015 03:15 AM

Quote:

Originally Posted by whim (Post 5312301)
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 > "

veerain 02-05-2015 03:18 AM

Quote:

Originally Posted by Miati (Post 5312306)
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 ).

whim 02-05-2015 05:07 AM

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.

veerain 02-05-2015 08:03 AM

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.

whim 02-05-2015 09:01 AM

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?

weibullguy 02-05-2015 09:29 AM

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.


All times are GMT -5. The time now is 02:39 PM.