Dot files
From charlesreid1
Dot files on Unix systems are (at first) straightforward and easy to use. However, as you dive deeper, they can get rather complicated, especially as you try to get various chains of dot-files working on various platforms. Because the chain of dot files calling one another varies from one operating system to another, my general approach is to ensure a call chain of my own, or put echo statements into each dot file to figure out what the existing call chain is. This is typically (assuming only a bash shell):
<graphviz> digraph G { rankdir="LR"
oncepershell [label="Once per shell", shape=square] onceperlogin [label="Once per login", shape=square]
"~/.profile" [URL=".profile", style=filled, fillcolor=lightblue] "~/.bash_profile" [URL=".bash_profile", style=filled, fillcolor=lightblue]
"~/.bashrc" [URL=".bashrc", style=filled, fillcolor=lightblue] "~/.aliases" [URL=".aliases", style=filled, fillcolor=lightblue]
onceperlogin -> "/etc/profile" "/etc/profile" -> "~/.profile" "/etc/profile" -> "~/.bash_profile"
oncepershell -> "/etc/bashrc" "/etc/bashrc" -> "~/.bashrc" "~/.bashrc" -> "~/.aliases" } </graphviz>
/etc/profile
I will begin with the system-level dot files. The file doesn't have a dot in the name, but it's still a dot file due to the syntax.
Any time any user logs in, their shell will source /etc/profile
. The /etc/profile
file contains initializations, environmental variables, and other initialization-related things that should be performed once each time the user logs in. This will contain really top-level stuff, like the name of the machine, the shell prompt text for all users and for super-users (sudo), etc.
/etc/profile
should also source ~/.profile
(the file named .profile
located in each users' home directory), and /etc/bashrc
(or something like this - it depends on your operating system and your shell).
/etc/bashrc
Like /etc/profile
, this file is sourced each time the user logs in; however, it is also sourced each time the user opens a new shell (either locally or remotely). This is where you would set bash-specific settings, such as turning on tab completion (source /etc/bash_completion
), making bash check window sizes after running commands (shopt -s checkwinsize
), appending new history to existing history (shopt -s histappend
and shopt -s cmdhist
), etc.
.profile
The user analog of the /etc/profile
, this file is useful for setting system variables, options, etc. that apply only to the user. This file does not require administrative privileges to edit, and takes precedence over any variables or options set in /etc/profile
(since it is sourced by /etc/profile
).
.bashrc
The .bashrc
file is the place to define any bash-specific variables, settings, aliases, etc. Sometimes this can become bloated, so it can be split into sections by putting different parts into different files, and sourcing those files from the .bashrc
file. For example, if I have a huge list of aliases (which I do), I can put those in a file in my home directory called .aliases
, and in my .bashrc
I put:
source ~/.aliases
and in my .aliases
I have my aliases defined:
alias back='cd $OLDPWD'
alias u='cd ..'
.bash_profile
This is where (more) bash-related options, variables, etc. are set. The file is like .profile
in that it is only sourced once per login session, but it is like .bashrc
in that it only sets options for the bash shell. This is the place to put things like history options; you can set the maximum size of the file containing your bash history, or the maximum number of history commands for bash to remember, etc.
HISTFILESIZE=1000000000
HISTSIZE=1000000
HISTTIMEFORMAT=': %Y-%m-%d_%H:%M:%S; '
shopt -s histappend