What is the .bashrc file?

106

43

Unix shells when starting read the .bashrc file and execute commands written in it. What is this file and what does it execute?

pineapple

Posted 2009-09-30T15:54:44.903

Reputation: 1 724

2Try opening the file in a text editor - it is in plain text. – Will Bickford – 2009-09-30T15:58:15.347

Answers

69

Actually, it's bash specifically that reads .bashrc (and /etc/bash.bashrc). There are lots of different shells.

The bash man page (by Brian Fox and Chet Ramey; also info page "Bash Startup Files") is the authoritative reference:

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. 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 ~/.bashrc.

When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi  

but the value of the PATH variable is not used to search for the file name.

The file is just shell commands. It is typically used to change prompts, set environment variables, and define shell procedures. Traditionally, the file .profile is used for this purpose, but bash has so many extensions that it needs its own startup file for users that want to put bashisms in startup files.

"Not a login shell" means things like script launches and usually terminal windows started by window managers. Sometimes I set up *nix systems to have .bashrc and BASH_ENV just source .profile. As long as you don't stray outside of POSIX shell commands then you will get the same initialization in any shell.

It's particularly valuable when sh is really bash, which sometimes happens. To do this use:

. .profile

One reason this is all so complex is because sometimes people put things that produce output into shell startup files, or they unconditionally set prompts. This causes lots of problems when running shell programs and backtick commands within languages, not to mention system(3) from C programs. The way bash starts up is designed, I think, to have one file where output and prompt setting is OK and one file where it isn't. Traditionally, a run-time test would be done to distinguish interactivity, for example, checking to see if the prompt is set.

DigitalRoss

Posted 2009-09-30T15:54:44.903

Reputation: 2 968

2Does anyone know what the rc part of .bashrc/.zshrc means? – WORMSS – 2015-06-19T13:28:04.497

4

@WORMSS: Good question! You're not the only one wondering about that.

– Caleb Xu – 2015-08-18T03:35:41.727

Maybe rc means runtime configuration. – Mike Diehn – 2018-11-28T21:59:26.997

rc in the .bashrc or .zshrc or any other means run commands – Mr. Suryaa Jha – 2020-02-16T14:27:46.773

Good answer. Just some quotation: The file is just shell commands. It is typically used to change prompts, set environment variables, and define shell procedures. – smwikipedia – 2011-02-19T03:53:19.557

12

When Bash starts, it executes the commands in a variety of different scripts.

When Bash is invoked as an interactive login shell, it first reads and executes commands 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.

When a login shell exits, Bash reads and executes commands from the file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists. 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 ~/.bashrc.

http://en.wikipedia.org/wiki/Bash_(Unix_shell)

Here are some tricks and tips:

http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html

Let us try to set the prompt so that it can display today’d date and hostname:

PS1="\d \h $ "

randomx

Posted 2009-09-30T15:54:44.903

Reputation: 321

1Why is .profile after .bash_profile? Any logic? – Pacerier – 2017-11-02T12:58:22.177

10

It should contain various "initialization" commands for your shell, e.g.:

  • Creating useful aliases (for example alias ll='ls -l').
  • Adding more directories to PATH.
  • Setting new environment variables.

Roman Zeyde

Posted 2009-09-30T15:54:44.903

Reputation: 101

2@pineapple: An alias is a way to run a command, which may be long, with a shorter one. For instance, the alias ll='ls -l' lets you type ll to execute the ls -l command. – Wuffers – 2011-02-19T03:53:56.800

What is use of alias? – pineapple – 2009-09-30T16:08:26.317