What is the purpose of .bashrc and how does it work?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
110
down vote

favorite
64












I found the .bashrc file and I want to know the purpose/function of it. Also how and when is it used?










share|improve this question



















  • 7




    superuser.com/questions/49289/what-is-the-bashrc-file
    – devnull
    May 13 '14 at 5:49














up vote
110
down vote

favorite
64












I found the .bashrc file and I want to know the purpose/function of it. Also how and when is it used?










share|improve this question



















  • 7




    superuser.com/questions/49289/what-is-the-bashrc-file
    – devnull
    May 13 '14 at 5:49












up vote
110
down vote

favorite
64









up vote
110
down vote

favorite
64






64





I found the .bashrc file and I want to know the purpose/function of it. Also how and when is it used?










share|improve this question















I found the .bashrc file and I want to know the purpose/function of it. Also how and when is it used?







bash bashrc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 13 '14 at 23:13









Gilles

524k12610471577




524k12610471577










asked May 13 '14 at 5:48









Pandya

8,3991348102




8,3991348102







  • 7




    superuser.com/questions/49289/what-is-the-bashrc-file
    – devnull
    May 13 '14 at 5:49












  • 7




    superuser.com/questions/49289/what-is-the-bashrc-file
    – devnull
    May 13 '14 at 5:49







7




7




superuser.com/questions/49289/what-is-the-bashrc-file
– devnull
May 13 '14 at 5:49




superuser.com/questions/49289/what-is-the-bashrc-file
– devnull
May 13 '14 at 5:49










3 Answers
3






active

oldest

votes

















up vote
101
down vote



accepted










.bashrc is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. You can put any command in that file that you could type at the command prompt.



You put commands here to set up the shell for use in your particular environment, or to customize things to your preferences. A common thing to put in .bashrc are aliases that you want to always be available.



.bashrc runs on every interactive shell launch. If you say:



$ bash ; bash ; bash


and then hit Ctrl-D three times, .bashrc will run three times. But if you say this instead:



$ bash -c exit ; bash -c exit ; bash -c exit


then .bashrc won't run at all, since -c makes the Bash call non-interactive. The same is true when you run a shell script from a file.



Contrast .bash_profile and .profile which are only run at the start of a new login shell. (bash -l) You choose whether a command goes in .bashrc vs .bash_profile depending on on whether you want it to run once or for every interactive shell start.



As a counterexample to aliases, which I prefer to put in .bashrc, you want to do PATH adjustments in .bash_profile instead, since these changes are typically not idempotent:



export PATH="$PATH:/some/addition"


If you put that in .bashrc instead, every time you launched an interactive sub-shell, :/some/addition would get tacked on to the end of the PATH again, creating extra work for the shell when you mistype a command.



You get a new interactive Bash shell whenever you shell out of vi with :sh, for example.






share|improve this answer


















  • 5




    Minor quibble: unlike most other shells, bash does not automatically load the per-instance config file .bashrc when it's started as a login shell. This can sometimes lead to unexpected behavior. The usual workaround is to source .bashrc from .profile or .bash_profile instead.
    – Ilmari Karonen
    May 13 '14 at 11:14











  • @IlmariKaronen Since .bashrc isn't intended for use by other shells, it's better not to source it from .profile (which might be used by other non-bash shells).
    – chepner
    May 14 '14 at 2:32










  • @IlmariKaronen what happens (read: how can I debug why) when source ~/.bashrc or . ~/.bashrc or [[ -f ~/.bashrc ]] && source. ~/.bashrc or [ -f ~/.bashrc ] && source ~/.bashrc don't work?
    – Nikos Alexandris
    Feb 12 '17 at 14:22











  • @NikosAlexandris: That's a separate question. Please ask it separately.
    – Warren Young
    Feb 12 '17 at 14:36










  • @WarrenYoung I would but I realised that .bashrc is indeed sourced (using the recommended command from superuser.com/a/183980/128768). The problem I face is actually the not sourcing of https://github.com/junegunn/fzf via this command inside .bashrc: source ~/.fzf.bash. I launch a new terminal, then Ctrl+R and I get `__fzf_history__`bash: fzf: command not found. I source manually .bashrc and the same key shortcut launches fzf as expected. Apologies for the noise here.
    – Nikos Alexandris
    Feb 12 '17 at 15:46


















up vote
19
down vote













The purpose of a .bashrc file is to provide a place where you can set up variables, functions and aliases, define your (PS1) prompt and define other settings that you want to use every start you open a new terminal window.



It works by being run each time you open up a new terminal, window or pane.



You can see mine here (pic with syntax highlighting):



HISTCONTROL=ignoreboth:erasedups HISTSIZE=100000 HISTFILESIZE=200000
ls --color=al > /dev/null 2>&1 && alias ls='ls -F --color=al' || alias ls='ls -G'
md () echo "Error - no directory passed!";
git_branch () git branch 2> /dev/null
HOST='33[02;36m]h'; HOST=' '$HOST
TIME='33[01;31m]t 33[01;32m]'
LOCATION=' 33[01;34m]`pwd | sed "s#(/[^/]1,/[^/]1,/[^/]1,/).*(/[^/]1,/[^/]1,)/0,1#1_2#g"`'
BRANCH=' 33[00;33m]$(git_branch)[33[00m]n$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
PS2='[33[01;36m]>'
set -o vi # vi at command line
export EDITOR=vim
test -f ~/.bash_aliases && . $_
test -f ~/.git-completion.bash && . $_
test -s ~/.autojump/etc/profile.d/autojump && . $_
[ $BASH_VERSINFO[0] -ge 4 ] && shopt -s autocd
[ -f /etc/bash_completion ] && ! shopt -oq posix && . /etc/bash_completion
[ -z $TMUX ] && export TERM=xterm-256color && exec tmux
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$home/.rvm/scripts/rvm"


Explanation:



-1. Set up my history file to ignore duplicates and be much larger than the default.

-2. Color option for ls depending on if you are using linux or OSX

-3. Function "md" to make and cd into a directory with one command

-4. Find the current git branch if in a git repo and...

-5. -9. Define an awesome PS1 prompt, as in <code>20:00:43 durrantm Castle2012 /hom/durrantm/Dropnot/_/rails_apps/linker master n $ _</code>

-10. Improved PS2 prompt

-11. Set vi as the editor at the command line

-12. Set vi as the default editor

-13. execute my .bash_aliases file if it exists

-14. Execute my git tab completion script (for remotes and branches) if it exists.

-15. Execute autojump if it exists

-16. Allow cd'ing without typing the cd part if the bash version >= 4

-17. Execute a bash completion script if it exists

-18. Use TMUX if it is present

-19. Add rvm to my PATH

-20. Use rvm if it exists.



I've made this portable so that it works on any of my linux or OSX machines without customization - hence a number of tests for presence are done before using certain functions and other scripts.



This also makes it easier to use the entire file immediately on a new machine without having issues that affect
opening a new terminal window.






share|improve this answer






















  • Overkill...       ͏͏͏͏͏͏͏͏͏
    – Pacerier
    Nov 2 '17 at 13:42










  • huh? too helpful for you?
    – Michael Durrant
    Nov 3 '17 at 2:12






  • 1




    Thank you SO much for sharing. A wealth of information here.
    – Vic
    Jul 3 at 15:28

















up vote
3
down vote













It is a bash config file.



Interactive (non-login) shells, then the config is read from these files:



  • $HOME/.bashrc

For Login shells, the config is read from these files:




  • /etc/profile (Always sourced)


  • $HOME/.bash_profile (the rest of these files are checked in order until one is found, then no others are read)

  • $HOME/.bash_login

  • $HOME/.profile

Simple illustration of how/when they are loaded is in the image below.



I added an echo to my .bashrc and .bash_profile



Interactive shell vs login shell



see man bash for more information






share|improve this answer


















  • 2




    This doesn't really add additional value to an old question. Also, I see that you're new to Unix & Linux, try not to post pictures of text, instead it's better to use the code block formatting and copy from your console. This allows the text to be searched/copied/etc. In the case of your example there aren't really any complicated commands or anything that would be good to search, but a good practice for other answers.
    – Centimane
    Sep 15 '16 at 18:24






  • 1




    I'm not sure I fully understand your reference to 'old question' It is in the top question list, and it is relevant with 60k views. I thought the added value came from the format, as opposed to a dialog/conjecture like the previous answers. but the format has been edited as well. I will keep in mind your comments about using code blocks, thanks.
    – Timothy L.J. Stewart
    Sep 15 '16 at 19:31






  • 2




    It's in the top of the 'active' question list because you posted an answer. The question was originally asked in 2014, and accepted an answer. The accepted answer states the purpose of .bashrc and distinguishes the difference between .bashrc and .bash_profile which covers the content of your answer. Don't be discouraged, you put forward an honest effort, but try to focus on answering questions with some content not covered by other answers.
    – Centimane
    Sep 15 '16 at 19:36











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f129143%2fwhat-is-the-purpose-of-bashrc-and-how-does-it-work%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
101
down vote



accepted










.bashrc is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. You can put any command in that file that you could type at the command prompt.



You put commands here to set up the shell for use in your particular environment, or to customize things to your preferences. A common thing to put in .bashrc are aliases that you want to always be available.



.bashrc runs on every interactive shell launch. If you say:



$ bash ; bash ; bash


and then hit Ctrl-D three times, .bashrc will run three times. But if you say this instead:



$ bash -c exit ; bash -c exit ; bash -c exit


then .bashrc won't run at all, since -c makes the Bash call non-interactive. The same is true when you run a shell script from a file.



Contrast .bash_profile and .profile which are only run at the start of a new login shell. (bash -l) You choose whether a command goes in .bashrc vs .bash_profile depending on on whether you want it to run once or for every interactive shell start.



As a counterexample to aliases, which I prefer to put in .bashrc, you want to do PATH adjustments in .bash_profile instead, since these changes are typically not idempotent:



export PATH="$PATH:/some/addition"


If you put that in .bashrc instead, every time you launched an interactive sub-shell, :/some/addition would get tacked on to the end of the PATH again, creating extra work for the shell when you mistype a command.



You get a new interactive Bash shell whenever you shell out of vi with :sh, for example.






share|improve this answer


















  • 5




    Minor quibble: unlike most other shells, bash does not automatically load the per-instance config file .bashrc when it's started as a login shell. This can sometimes lead to unexpected behavior. The usual workaround is to source .bashrc from .profile or .bash_profile instead.
    – Ilmari Karonen
    May 13 '14 at 11:14











  • @IlmariKaronen Since .bashrc isn't intended for use by other shells, it's better not to source it from .profile (which might be used by other non-bash shells).
    – chepner
    May 14 '14 at 2:32










  • @IlmariKaronen what happens (read: how can I debug why) when source ~/.bashrc or . ~/.bashrc or [[ -f ~/.bashrc ]] && source. ~/.bashrc or [ -f ~/.bashrc ] && source ~/.bashrc don't work?
    – Nikos Alexandris
    Feb 12 '17 at 14:22











  • @NikosAlexandris: That's a separate question. Please ask it separately.
    – Warren Young
    Feb 12 '17 at 14:36










  • @WarrenYoung I would but I realised that .bashrc is indeed sourced (using the recommended command from superuser.com/a/183980/128768). The problem I face is actually the not sourcing of https://github.com/junegunn/fzf via this command inside .bashrc: source ~/.fzf.bash. I launch a new terminal, then Ctrl+R and I get `__fzf_history__`bash: fzf: command not found. I source manually .bashrc and the same key shortcut launches fzf as expected. Apologies for the noise here.
    – Nikos Alexandris
    Feb 12 '17 at 15:46















up vote
101
down vote



accepted










.bashrc is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. You can put any command in that file that you could type at the command prompt.



You put commands here to set up the shell for use in your particular environment, or to customize things to your preferences. A common thing to put in .bashrc are aliases that you want to always be available.



.bashrc runs on every interactive shell launch. If you say:



$ bash ; bash ; bash


and then hit Ctrl-D three times, .bashrc will run three times. But if you say this instead:



$ bash -c exit ; bash -c exit ; bash -c exit


then .bashrc won't run at all, since -c makes the Bash call non-interactive. The same is true when you run a shell script from a file.



Contrast .bash_profile and .profile which are only run at the start of a new login shell. (bash -l) You choose whether a command goes in .bashrc vs .bash_profile depending on on whether you want it to run once or for every interactive shell start.



As a counterexample to aliases, which I prefer to put in .bashrc, you want to do PATH adjustments in .bash_profile instead, since these changes are typically not idempotent:



export PATH="$PATH:/some/addition"


If you put that in .bashrc instead, every time you launched an interactive sub-shell, :/some/addition would get tacked on to the end of the PATH again, creating extra work for the shell when you mistype a command.



You get a new interactive Bash shell whenever you shell out of vi with :sh, for example.






share|improve this answer


















  • 5




    Minor quibble: unlike most other shells, bash does not automatically load the per-instance config file .bashrc when it's started as a login shell. This can sometimes lead to unexpected behavior. The usual workaround is to source .bashrc from .profile or .bash_profile instead.
    – Ilmari Karonen
    May 13 '14 at 11:14











  • @IlmariKaronen Since .bashrc isn't intended for use by other shells, it's better not to source it from .profile (which might be used by other non-bash shells).
    – chepner
    May 14 '14 at 2:32










  • @IlmariKaronen what happens (read: how can I debug why) when source ~/.bashrc or . ~/.bashrc or [[ -f ~/.bashrc ]] && source. ~/.bashrc or [ -f ~/.bashrc ] && source ~/.bashrc don't work?
    – Nikos Alexandris
    Feb 12 '17 at 14:22











  • @NikosAlexandris: That's a separate question. Please ask it separately.
    – Warren Young
    Feb 12 '17 at 14:36










  • @WarrenYoung I would but I realised that .bashrc is indeed sourced (using the recommended command from superuser.com/a/183980/128768). The problem I face is actually the not sourcing of https://github.com/junegunn/fzf via this command inside .bashrc: source ~/.fzf.bash. I launch a new terminal, then Ctrl+R and I get `__fzf_history__`bash: fzf: command not found. I source manually .bashrc and the same key shortcut launches fzf as expected. Apologies for the noise here.
    – Nikos Alexandris
    Feb 12 '17 at 15:46













up vote
101
down vote



accepted







up vote
101
down vote



accepted






.bashrc is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. You can put any command in that file that you could type at the command prompt.



You put commands here to set up the shell for use in your particular environment, or to customize things to your preferences. A common thing to put in .bashrc are aliases that you want to always be available.



.bashrc runs on every interactive shell launch. If you say:



$ bash ; bash ; bash


and then hit Ctrl-D three times, .bashrc will run three times. But if you say this instead:



$ bash -c exit ; bash -c exit ; bash -c exit


then .bashrc won't run at all, since -c makes the Bash call non-interactive. The same is true when you run a shell script from a file.



Contrast .bash_profile and .profile which are only run at the start of a new login shell. (bash -l) You choose whether a command goes in .bashrc vs .bash_profile depending on on whether you want it to run once or for every interactive shell start.



As a counterexample to aliases, which I prefer to put in .bashrc, you want to do PATH adjustments in .bash_profile instead, since these changes are typically not idempotent:



export PATH="$PATH:/some/addition"


If you put that in .bashrc instead, every time you launched an interactive sub-shell, :/some/addition would get tacked on to the end of the PATH again, creating extra work for the shell when you mistype a command.



You get a new interactive Bash shell whenever you shell out of vi with :sh, for example.






share|improve this answer














.bashrc is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. You can put any command in that file that you could type at the command prompt.



You put commands here to set up the shell for use in your particular environment, or to customize things to your preferences. A common thing to put in .bashrc are aliases that you want to always be available.



.bashrc runs on every interactive shell launch. If you say:



$ bash ; bash ; bash


and then hit Ctrl-D three times, .bashrc will run three times. But if you say this instead:



$ bash -c exit ; bash -c exit ; bash -c exit


then .bashrc won't run at all, since -c makes the Bash call non-interactive. The same is true when you run a shell script from a file.



Contrast .bash_profile and .profile which are only run at the start of a new login shell. (bash -l) You choose whether a command goes in .bashrc vs .bash_profile depending on on whether you want it to run once or for every interactive shell start.



As a counterexample to aliases, which I prefer to put in .bashrc, you want to do PATH adjustments in .bash_profile instead, since these changes are typically not idempotent:



export PATH="$PATH:/some/addition"


If you put that in .bashrc instead, every time you launched an interactive sub-shell, :/some/addition would get tacked on to the end of the PATH again, creating extra work for the shell when you mistype a command.



You get a new interactive Bash shell whenever you shell out of vi with :sh, for example.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 13 at 7:35









Kusalananda

118k16223364




118k16223364










answered May 13 '14 at 6:02









Warren Young

54.3k9142145




54.3k9142145







  • 5




    Minor quibble: unlike most other shells, bash does not automatically load the per-instance config file .bashrc when it's started as a login shell. This can sometimes lead to unexpected behavior. The usual workaround is to source .bashrc from .profile or .bash_profile instead.
    – Ilmari Karonen
    May 13 '14 at 11:14











  • @IlmariKaronen Since .bashrc isn't intended for use by other shells, it's better not to source it from .profile (which might be used by other non-bash shells).
    – chepner
    May 14 '14 at 2:32










  • @IlmariKaronen what happens (read: how can I debug why) when source ~/.bashrc or . ~/.bashrc or [[ -f ~/.bashrc ]] && source. ~/.bashrc or [ -f ~/.bashrc ] && source ~/.bashrc don't work?
    – Nikos Alexandris
    Feb 12 '17 at 14:22











  • @NikosAlexandris: That's a separate question. Please ask it separately.
    – Warren Young
    Feb 12 '17 at 14:36










  • @WarrenYoung I would but I realised that .bashrc is indeed sourced (using the recommended command from superuser.com/a/183980/128768). The problem I face is actually the not sourcing of https://github.com/junegunn/fzf via this command inside .bashrc: source ~/.fzf.bash. I launch a new terminal, then Ctrl+R and I get `__fzf_history__`bash: fzf: command not found. I source manually .bashrc and the same key shortcut launches fzf as expected. Apologies for the noise here.
    – Nikos Alexandris
    Feb 12 '17 at 15:46













  • 5




    Minor quibble: unlike most other shells, bash does not automatically load the per-instance config file .bashrc when it's started as a login shell. This can sometimes lead to unexpected behavior. The usual workaround is to source .bashrc from .profile or .bash_profile instead.
    – Ilmari Karonen
    May 13 '14 at 11:14











  • @IlmariKaronen Since .bashrc isn't intended for use by other shells, it's better not to source it from .profile (which might be used by other non-bash shells).
    – chepner
    May 14 '14 at 2:32










  • @IlmariKaronen what happens (read: how can I debug why) when source ~/.bashrc or . ~/.bashrc or [[ -f ~/.bashrc ]] && source. ~/.bashrc or [ -f ~/.bashrc ] && source ~/.bashrc don't work?
    – Nikos Alexandris
    Feb 12 '17 at 14:22











  • @NikosAlexandris: That's a separate question. Please ask it separately.
    – Warren Young
    Feb 12 '17 at 14:36










  • @WarrenYoung I would but I realised that .bashrc is indeed sourced (using the recommended command from superuser.com/a/183980/128768). The problem I face is actually the not sourcing of https://github.com/junegunn/fzf via this command inside .bashrc: source ~/.fzf.bash. I launch a new terminal, then Ctrl+R and I get `__fzf_history__`bash: fzf: command not found. I source manually .bashrc and the same key shortcut launches fzf as expected. Apologies for the noise here.
    – Nikos Alexandris
    Feb 12 '17 at 15:46








5




5




Minor quibble: unlike most other shells, bash does not automatically load the per-instance config file .bashrc when it's started as a login shell. This can sometimes lead to unexpected behavior. The usual workaround is to source .bashrc from .profile or .bash_profile instead.
– Ilmari Karonen
May 13 '14 at 11:14





Minor quibble: unlike most other shells, bash does not automatically load the per-instance config file .bashrc when it's started as a login shell. This can sometimes lead to unexpected behavior. The usual workaround is to source .bashrc from .profile or .bash_profile instead.
– Ilmari Karonen
May 13 '14 at 11:14













@IlmariKaronen Since .bashrc isn't intended for use by other shells, it's better not to source it from .profile (which might be used by other non-bash shells).
– chepner
May 14 '14 at 2:32




@IlmariKaronen Since .bashrc isn't intended for use by other shells, it's better not to source it from .profile (which might be used by other non-bash shells).
– chepner
May 14 '14 at 2:32












@IlmariKaronen what happens (read: how can I debug why) when source ~/.bashrc or . ~/.bashrc or [[ -f ~/.bashrc ]] && source. ~/.bashrc or [ -f ~/.bashrc ] && source ~/.bashrc don't work?
– Nikos Alexandris
Feb 12 '17 at 14:22





@IlmariKaronen what happens (read: how can I debug why) when source ~/.bashrc or . ~/.bashrc or [[ -f ~/.bashrc ]] && source. ~/.bashrc or [ -f ~/.bashrc ] && source ~/.bashrc don't work?
– Nikos Alexandris
Feb 12 '17 at 14:22













@NikosAlexandris: That's a separate question. Please ask it separately.
– Warren Young
Feb 12 '17 at 14:36




@NikosAlexandris: That's a separate question. Please ask it separately.
– Warren Young
Feb 12 '17 at 14:36












@WarrenYoung I would but I realised that .bashrc is indeed sourced (using the recommended command from superuser.com/a/183980/128768). The problem I face is actually the not sourcing of https://github.com/junegunn/fzf via this command inside .bashrc: source ~/.fzf.bash. I launch a new terminal, then Ctrl+R and I get `__fzf_history__`bash: fzf: command not found. I source manually .bashrc and the same key shortcut launches fzf as expected. Apologies for the noise here.
– Nikos Alexandris
Feb 12 '17 at 15:46





@WarrenYoung I would but I realised that .bashrc is indeed sourced (using the recommended command from superuser.com/a/183980/128768). The problem I face is actually the not sourcing of https://github.com/junegunn/fzf via this command inside .bashrc: source ~/.fzf.bash. I launch a new terminal, then Ctrl+R and I get `__fzf_history__`bash: fzf: command not found. I source manually .bashrc and the same key shortcut launches fzf as expected. Apologies for the noise here.
– Nikos Alexandris
Feb 12 '17 at 15:46













up vote
19
down vote













The purpose of a .bashrc file is to provide a place where you can set up variables, functions and aliases, define your (PS1) prompt and define other settings that you want to use every start you open a new terminal window.



It works by being run each time you open up a new terminal, window or pane.



You can see mine here (pic with syntax highlighting):



HISTCONTROL=ignoreboth:erasedups HISTSIZE=100000 HISTFILESIZE=200000
ls --color=al > /dev/null 2>&1 && alias ls='ls -F --color=al' || alias ls='ls -G'
md () echo "Error - no directory passed!";
git_branch () git branch 2> /dev/null
HOST='33[02;36m]h'; HOST=' '$HOST
TIME='33[01;31m]t 33[01;32m]'
LOCATION=' 33[01;34m]`pwd | sed "s#(/[^/]1,/[^/]1,/[^/]1,/).*(/[^/]1,/[^/]1,)/0,1#1_2#g"`'
BRANCH=' 33[00;33m]$(git_branch)[33[00m]n$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
PS2='[33[01;36m]>'
set -o vi # vi at command line
export EDITOR=vim
test -f ~/.bash_aliases && . $_
test -f ~/.git-completion.bash && . $_
test -s ~/.autojump/etc/profile.d/autojump && . $_
[ $BASH_VERSINFO[0] -ge 4 ] && shopt -s autocd
[ -f /etc/bash_completion ] && ! shopt -oq posix && . /etc/bash_completion
[ -z $TMUX ] && export TERM=xterm-256color && exec tmux
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$home/.rvm/scripts/rvm"


Explanation:



-1. Set up my history file to ignore duplicates and be much larger than the default.

-2. Color option for ls depending on if you are using linux or OSX

-3. Function "md" to make and cd into a directory with one command

-4. Find the current git branch if in a git repo and...

-5. -9. Define an awesome PS1 prompt, as in <code>20:00:43 durrantm Castle2012 /hom/durrantm/Dropnot/_/rails_apps/linker master n $ _</code>

-10. Improved PS2 prompt

-11. Set vi as the editor at the command line

-12. Set vi as the default editor

-13. execute my .bash_aliases file if it exists

-14. Execute my git tab completion script (for remotes and branches) if it exists.

-15. Execute autojump if it exists

-16. Allow cd'ing without typing the cd part if the bash version >= 4

-17. Execute a bash completion script if it exists

-18. Use TMUX if it is present

-19. Add rvm to my PATH

-20. Use rvm if it exists.



I've made this portable so that it works on any of my linux or OSX machines without customization - hence a number of tests for presence are done before using certain functions and other scripts.



This also makes it easier to use the entire file immediately on a new machine without having issues that affect
opening a new terminal window.






share|improve this answer






















  • Overkill...       ͏͏͏͏͏͏͏͏͏
    – Pacerier
    Nov 2 '17 at 13:42










  • huh? too helpful for you?
    – Michael Durrant
    Nov 3 '17 at 2:12






  • 1




    Thank you SO much for sharing. A wealth of information here.
    – Vic
    Jul 3 at 15:28














up vote
19
down vote













The purpose of a .bashrc file is to provide a place where you can set up variables, functions and aliases, define your (PS1) prompt and define other settings that you want to use every start you open a new terminal window.



It works by being run each time you open up a new terminal, window or pane.



You can see mine here (pic with syntax highlighting):



HISTCONTROL=ignoreboth:erasedups HISTSIZE=100000 HISTFILESIZE=200000
ls --color=al > /dev/null 2>&1 && alias ls='ls -F --color=al' || alias ls='ls -G'
md () echo "Error - no directory passed!";
git_branch () git branch 2> /dev/null
HOST='33[02;36m]h'; HOST=' '$HOST
TIME='33[01;31m]t 33[01;32m]'
LOCATION=' 33[01;34m]`pwd | sed "s#(/[^/]1,/[^/]1,/[^/]1,/).*(/[^/]1,/[^/]1,)/0,1#1_2#g"`'
BRANCH=' 33[00;33m]$(git_branch)[33[00m]n$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
PS2='[33[01;36m]>'
set -o vi # vi at command line
export EDITOR=vim
test -f ~/.bash_aliases && . $_
test -f ~/.git-completion.bash && . $_
test -s ~/.autojump/etc/profile.d/autojump && . $_
[ $BASH_VERSINFO[0] -ge 4 ] && shopt -s autocd
[ -f /etc/bash_completion ] && ! shopt -oq posix && . /etc/bash_completion
[ -z $TMUX ] && export TERM=xterm-256color && exec tmux
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$home/.rvm/scripts/rvm"


Explanation:



-1. Set up my history file to ignore duplicates and be much larger than the default.

-2. Color option for ls depending on if you are using linux or OSX

-3. Function "md" to make and cd into a directory with one command

-4. Find the current git branch if in a git repo and...

-5. -9. Define an awesome PS1 prompt, as in <code>20:00:43 durrantm Castle2012 /hom/durrantm/Dropnot/_/rails_apps/linker master n $ _</code>

-10. Improved PS2 prompt

-11. Set vi as the editor at the command line

-12. Set vi as the default editor

-13. execute my .bash_aliases file if it exists

-14. Execute my git tab completion script (for remotes and branches) if it exists.

-15. Execute autojump if it exists

-16. Allow cd'ing without typing the cd part if the bash version >= 4

-17. Execute a bash completion script if it exists

-18. Use TMUX if it is present

-19. Add rvm to my PATH

-20. Use rvm if it exists.



I've made this portable so that it works on any of my linux or OSX machines without customization - hence a number of tests for presence are done before using certain functions and other scripts.



This also makes it easier to use the entire file immediately on a new machine without having issues that affect
opening a new terminal window.






share|improve this answer






















  • Overkill...       ͏͏͏͏͏͏͏͏͏
    – Pacerier
    Nov 2 '17 at 13:42










  • huh? too helpful for you?
    – Michael Durrant
    Nov 3 '17 at 2:12






  • 1




    Thank you SO much for sharing. A wealth of information here.
    – Vic
    Jul 3 at 15:28












up vote
19
down vote










up vote
19
down vote









The purpose of a .bashrc file is to provide a place where you can set up variables, functions and aliases, define your (PS1) prompt and define other settings that you want to use every start you open a new terminal window.



It works by being run each time you open up a new terminal, window or pane.



You can see mine here (pic with syntax highlighting):



HISTCONTROL=ignoreboth:erasedups HISTSIZE=100000 HISTFILESIZE=200000
ls --color=al > /dev/null 2>&1 && alias ls='ls -F --color=al' || alias ls='ls -G'
md () echo "Error - no directory passed!";
git_branch () git branch 2> /dev/null
HOST='33[02;36m]h'; HOST=' '$HOST
TIME='33[01;31m]t 33[01;32m]'
LOCATION=' 33[01;34m]`pwd | sed "s#(/[^/]1,/[^/]1,/[^/]1,/).*(/[^/]1,/[^/]1,)/0,1#1_2#g"`'
BRANCH=' 33[00;33m]$(git_branch)[33[00m]n$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
PS2='[33[01;36m]>'
set -o vi # vi at command line
export EDITOR=vim
test -f ~/.bash_aliases && . $_
test -f ~/.git-completion.bash && . $_
test -s ~/.autojump/etc/profile.d/autojump && . $_
[ $BASH_VERSINFO[0] -ge 4 ] && shopt -s autocd
[ -f /etc/bash_completion ] && ! shopt -oq posix && . /etc/bash_completion
[ -z $TMUX ] && export TERM=xterm-256color && exec tmux
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$home/.rvm/scripts/rvm"


Explanation:



-1. Set up my history file to ignore duplicates and be much larger than the default.

-2. Color option for ls depending on if you are using linux or OSX

-3. Function "md" to make and cd into a directory with one command

-4. Find the current git branch if in a git repo and...

-5. -9. Define an awesome PS1 prompt, as in <code>20:00:43 durrantm Castle2012 /hom/durrantm/Dropnot/_/rails_apps/linker master n $ _</code>

-10. Improved PS2 prompt

-11. Set vi as the editor at the command line

-12. Set vi as the default editor

-13. execute my .bash_aliases file if it exists

-14. Execute my git tab completion script (for remotes and branches) if it exists.

-15. Execute autojump if it exists

-16. Allow cd'ing without typing the cd part if the bash version >= 4

-17. Execute a bash completion script if it exists

-18. Use TMUX if it is present

-19. Add rvm to my PATH

-20. Use rvm if it exists.



I've made this portable so that it works on any of my linux or OSX machines without customization - hence a number of tests for presence are done before using certain functions and other scripts.



This also makes it easier to use the entire file immediately on a new machine without having issues that affect
opening a new terminal window.






share|improve this answer














The purpose of a .bashrc file is to provide a place where you can set up variables, functions and aliases, define your (PS1) prompt and define other settings that you want to use every start you open a new terminal window.



It works by being run each time you open up a new terminal, window or pane.



You can see mine here (pic with syntax highlighting):



HISTCONTROL=ignoreboth:erasedups HISTSIZE=100000 HISTFILESIZE=200000
ls --color=al > /dev/null 2>&1 && alias ls='ls -F --color=al' || alias ls='ls -G'
md () echo "Error - no directory passed!";
git_branch () git branch 2> /dev/null
HOST='33[02;36m]h'; HOST=' '$HOST
TIME='33[01;31m]t 33[01;32m]'
LOCATION=' 33[01;34m]`pwd | sed "s#(/[^/]1,/[^/]1,/[^/]1,/).*(/[^/]1,/[^/]1,)/0,1#1_2#g"`'
BRANCH=' 33[00;33m]$(git_branch)[33[00m]n$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
PS2='[33[01;36m]>'
set -o vi # vi at command line
export EDITOR=vim
test -f ~/.bash_aliases && . $_
test -f ~/.git-completion.bash && . $_
test -s ~/.autojump/etc/profile.d/autojump && . $_
[ $BASH_VERSINFO[0] -ge 4 ] && shopt -s autocd
[ -f /etc/bash_completion ] && ! shopt -oq posix && . /etc/bash_completion
[ -z $TMUX ] && export TERM=xterm-256color && exec tmux
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$home/.rvm/scripts/rvm"


Explanation:



-1. Set up my history file to ignore duplicates and be much larger than the default.

-2. Color option for ls depending on if you are using linux or OSX

-3. Function "md" to make and cd into a directory with one command

-4. Find the current git branch if in a git repo and...

-5. -9. Define an awesome PS1 prompt, as in <code>20:00:43 durrantm Castle2012 /hom/durrantm/Dropnot/_/rails_apps/linker master n $ _</code>

-10. Improved PS2 prompt

-11. Set vi as the editor at the command line

-12. Set vi as the default editor

-13. execute my .bash_aliases file if it exists

-14. Execute my git tab completion script (for remotes and branches) if it exists.

-15. Execute autojump if it exists

-16. Allow cd'ing without typing the cd part if the bash version >= 4

-17. Execute a bash completion script if it exists

-18. Use TMUX if it is present

-19. Add rvm to my PATH

-20. Use rvm if it exists.



I've made this portable so that it works on any of my linux or OSX machines without customization - hence a number of tests for presence are done before using certain functions and other scripts.



This also makes it easier to use the entire file immediately on a new machine without having issues that affect
opening a new terminal window.







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 16 '17 at 19:12









Aaron Hall

349317




349317










answered May 13 '14 at 23:57









Michael Durrant

15.6k44112180




15.6k44112180











  • Overkill...       ͏͏͏͏͏͏͏͏͏
    – Pacerier
    Nov 2 '17 at 13:42










  • huh? too helpful for you?
    – Michael Durrant
    Nov 3 '17 at 2:12






  • 1




    Thank you SO much for sharing. A wealth of information here.
    – Vic
    Jul 3 at 15:28
















  • Overkill...       ͏͏͏͏͏͏͏͏͏
    – Pacerier
    Nov 2 '17 at 13:42










  • huh? too helpful for you?
    – Michael Durrant
    Nov 3 '17 at 2:12






  • 1




    Thank you SO much for sharing. A wealth of information here.
    – Vic
    Jul 3 at 15:28















Overkill...       ͏͏͏͏͏͏͏͏͏
– Pacerier
Nov 2 '17 at 13:42




Overkill...       ͏͏͏͏͏͏͏͏͏
– Pacerier
Nov 2 '17 at 13:42












huh? too helpful for you?
– Michael Durrant
Nov 3 '17 at 2:12




huh? too helpful for you?
– Michael Durrant
Nov 3 '17 at 2:12




1




1




Thank you SO much for sharing. A wealth of information here.
– Vic
Jul 3 at 15:28




Thank you SO much for sharing. A wealth of information here.
– Vic
Jul 3 at 15:28










up vote
3
down vote













It is a bash config file.



Interactive (non-login) shells, then the config is read from these files:



  • $HOME/.bashrc

For Login shells, the config is read from these files:




  • /etc/profile (Always sourced)


  • $HOME/.bash_profile (the rest of these files are checked in order until one is found, then no others are read)

  • $HOME/.bash_login

  • $HOME/.profile

Simple illustration of how/when they are loaded is in the image below.



I added an echo to my .bashrc and .bash_profile



Interactive shell vs login shell



see man bash for more information






share|improve this answer


















  • 2




    This doesn't really add additional value to an old question. Also, I see that you're new to Unix & Linux, try not to post pictures of text, instead it's better to use the code block formatting and copy from your console. This allows the text to be searched/copied/etc. In the case of your example there aren't really any complicated commands or anything that would be good to search, but a good practice for other answers.
    – Centimane
    Sep 15 '16 at 18:24






  • 1




    I'm not sure I fully understand your reference to 'old question' It is in the top question list, and it is relevant with 60k views. I thought the added value came from the format, as opposed to a dialog/conjecture like the previous answers. but the format has been edited as well. I will keep in mind your comments about using code blocks, thanks.
    – Timothy L.J. Stewart
    Sep 15 '16 at 19:31






  • 2




    It's in the top of the 'active' question list because you posted an answer. The question was originally asked in 2014, and accepted an answer. The accepted answer states the purpose of .bashrc and distinguishes the difference between .bashrc and .bash_profile which covers the content of your answer. Don't be discouraged, you put forward an honest effort, but try to focus on answering questions with some content not covered by other answers.
    – Centimane
    Sep 15 '16 at 19:36















up vote
3
down vote













It is a bash config file.



Interactive (non-login) shells, then the config is read from these files:



  • $HOME/.bashrc

For Login shells, the config is read from these files:




  • /etc/profile (Always sourced)


  • $HOME/.bash_profile (the rest of these files are checked in order until one is found, then no others are read)

  • $HOME/.bash_login

  • $HOME/.profile

Simple illustration of how/when they are loaded is in the image below.



I added an echo to my .bashrc and .bash_profile



Interactive shell vs login shell



see man bash for more information






share|improve this answer


















  • 2




    This doesn't really add additional value to an old question. Also, I see that you're new to Unix & Linux, try not to post pictures of text, instead it's better to use the code block formatting and copy from your console. This allows the text to be searched/copied/etc. In the case of your example there aren't really any complicated commands or anything that would be good to search, but a good practice for other answers.
    – Centimane
    Sep 15 '16 at 18:24






  • 1




    I'm not sure I fully understand your reference to 'old question' It is in the top question list, and it is relevant with 60k views. I thought the added value came from the format, as opposed to a dialog/conjecture like the previous answers. but the format has been edited as well. I will keep in mind your comments about using code blocks, thanks.
    – Timothy L.J. Stewart
    Sep 15 '16 at 19:31






  • 2




    It's in the top of the 'active' question list because you posted an answer. The question was originally asked in 2014, and accepted an answer. The accepted answer states the purpose of .bashrc and distinguishes the difference between .bashrc and .bash_profile which covers the content of your answer. Don't be discouraged, you put forward an honest effort, but try to focus on answering questions with some content not covered by other answers.
    – Centimane
    Sep 15 '16 at 19:36













up vote
3
down vote










up vote
3
down vote









It is a bash config file.



Interactive (non-login) shells, then the config is read from these files:



  • $HOME/.bashrc

For Login shells, the config is read from these files:




  • /etc/profile (Always sourced)


  • $HOME/.bash_profile (the rest of these files are checked in order until one is found, then no others are read)

  • $HOME/.bash_login

  • $HOME/.profile

Simple illustration of how/when they are loaded is in the image below.



I added an echo to my .bashrc and .bash_profile



Interactive shell vs login shell



see man bash for more information






share|improve this answer














It is a bash config file.



Interactive (non-login) shells, then the config is read from these files:



  • $HOME/.bashrc

For Login shells, the config is read from these files:




  • /etc/profile (Always sourced)


  • $HOME/.bash_profile (the rest of these files are checked in order until one is found, then no others are read)

  • $HOME/.bash_login

  • $HOME/.profile

Simple illustration of how/when they are loaded is in the image below.



I added an echo to my .bashrc and .bash_profile



Interactive shell vs login shell



see man bash for more information







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 15 '16 at 18:37









HalosGhost

3,67592135




3,67592135










answered Sep 15 '16 at 18:09









Timothy L.J. Stewart

1312




1312







  • 2




    This doesn't really add additional value to an old question. Also, I see that you're new to Unix & Linux, try not to post pictures of text, instead it's better to use the code block formatting and copy from your console. This allows the text to be searched/copied/etc. In the case of your example there aren't really any complicated commands or anything that would be good to search, but a good practice for other answers.
    – Centimane
    Sep 15 '16 at 18:24






  • 1




    I'm not sure I fully understand your reference to 'old question' It is in the top question list, and it is relevant with 60k views. I thought the added value came from the format, as opposed to a dialog/conjecture like the previous answers. but the format has been edited as well. I will keep in mind your comments about using code blocks, thanks.
    – Timothy L.J. Stewart
    Sep 15 '16 at 19:31






  • 2




    It's in the top of the 'active' question list because you posted an answer. The question was originally asked in 2014, and accepted an answer. The accepted answer states the purpose of .bashrc and distinguishes the difference between .bashrc and .bash_profile which covers the content of your answer. Don't be discouraged, you put forward an honest effort, but try to focus on answering questions with some content not covered by other answers.
    – Centimane
    Sep 15 '16 at 19:36













  • 2




    This doesn't really add additional value to an old question. Also, I see that you're new to Unix & Linux, try not to post pictures of text, instead it's better to use the code block formatting and copy from your console. This allows the text to be searched/copied/etc. In the case of your example there aren't really any complicated commands or anything that would be good to search, but a good practice for other answers.
    – Centimane
    Sep 15 '16 at 18:24






  • 1




    I'm not sure I fully understand your reference to 'old question' It is in the top question list, and it is relevant with 60k views. I thought the added value came from the format, as opposed to a dialog/conjecture like the previous answers. but the format has been edited as well. I will keep in mind your comments about using code blocks, thanks.
    – Timothy L.J. Stewart
    Sep 15 '16 at 19:31






  • 2




    It's in the top of the 'active' question list because you posted an answer. The question was originally asked in 2014, and accepted an answer. The accepted answer states the purpose of .bashrc and distinguishes the difference between .bashrc and .bash_profile which covers the content of your answer. Don't be discouraged, you put forward an honest effort, but try to focus on answering questions with some content not covered by other answers.
    – Centimane
    Sep 15 '16 at 19:36








2




2




This doesn't really add additional value to an old question. Also, I see that you're new to Unix & Linux, try not to post pictures of text, instead it's better to use the code block formatting and copy from your console. This allows the text to be searched/copied/etc. In the case of your example there aren't really any complicated commands or anything that would be good to search, but a good practice for other answers.
– Centimane
Sep 15 '16 at 18:24




This doesn't really add additional value to an old question. Also, I see that you're new to Unix & Linux, try not to post pictures of text, instead it's better to use the code block formatting and copy from your console. This allows the text to be searched/copied/etc. In the case of your example there aren't really any complicated commands or anything that would be good to search, but a good practice for other answers.
– Centimane
Sep 15 '16 at 18:24




1




1




I'm not sure I fully understand your reference to 'old question' It is in the top question list, and it is relevant with 60k views. I thought the added value came from the format, as opposed to a dialog/conjecture like the previous answers. but the format has been edited as well. I will keep in mind your comments about using code blocks, thanks.
– Timothy L.J. Stewart
Sep 15 '16 at 19:31




I'm not sure I fully understand your reference to 'old question' It is in the top question list, and it is relevant with 60k views. I thought the added value came from the format, as opposed to a dialog/conjecture like the previous answers. but the format has been edited as well. I will keep in mind your comments about using code blocks, thanks.
– Timothy L.J. Stewart
Sep 15 '16 at 19:31




2




2




It's in the top of the 'active' question list because you posted an answer. The question was originally asked in 2014, and accepted an answer. The accepted answer states the purpose of .bashrc and distinguishes the difference between .bashrc and .bash_profile which covers the content of your answer. Don't be discouraged, you put forward an honest effort, but try to focus on answering questions with some content not covered by other answers.
– Centimane
Sep 15 '16 at 19:36





It's in the top of the 'active' question list because you posted an answer. The question was originally asked in 2014, and accepted an answer. The accepted answer states the purpose of .bashrc and distinguishes the difference between .bashrc and .bash_profile which covers the content of your answer. Don't be discouraged, you put forward an honest effort, but try to focus on answering questions with some content not covered by other answers.
– Centimane
Sep 15 '16 at 19:36


















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f129143%2fwhat-is-the-purpose-of-bashrc-and-how-does-it-work%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay