$ grep stahl /etc/passwd stahl:x:25649:10120:Prof David Stahl:/users/faculty/stahl:/bin/bash $ echo $USER $UID $HOME $SHELL <-- shell variables! stahl 25649 /users/faculty/stahl /bin/bashSomehow those variables got initialized ( how ? ) When a UNIX system starts up, the program /sbin/init is run (it does initialization).
- init
- runs multiple instances of the program /etc/getty, one for every terminal through which a user can login (e.g.: console, modem, network)
- getty
- detects a connection and gives the login as: prompt, reads the login name you enter, runs the program /bin/login
- login
- gives the Password: prompt, reads the password you enter, examines /etc/passwd to set up your environment:
stahl:x:25649:10120:Prof David Stahl:/users/faculty/stahl:/bin/bash ^ ^ ^ | | | sets $UID $HOME, $SHELLthen runs the specified shell (/bin/bash in this example) with a special flag to let it know that this is a "login shell", i.e. the initial shell launched upon logging in.- bash
- (Note: to "source" a file means that the shell takes each line of the file and executes it as if it were typed in at the command-line. In fact, if file
foocontains a bunch of shell commands, the commandsource foocauses the shell to execute each of those commands as if they'd been typed in one by one.) Since bash has been launched with the login shell flag, itsources the system-wide script /etc/profile (if it exists) which provides a basic environment for everyone, then in this order looks in $HOME for, andsources one of:.bash_profile, .bash_login, .profile. This sets up your own personal environment. These files typically call:source $HOME/.bashrc(a file named .______rc is called a "resource file") which we'll discuss more later. Together, these files set up your environment: global environment variables, and the behavior of the shell. Finally, when you logout the file~/.bash_logout(if it exists) getssourced.
/bin/bash processes running. If only one
resulted from logging in, how did they get there?
How do they get intialized, since the login initialization
doesn't happen?
First of all, when you launch a new xterm, start a bash
shell up in xemacs, or execute a bash-shell script (i.e. a
text file that begins with #!/bin/bash, a thing
which we'll get to next lecture), a new
bash-shell process is created ... and not a login shell.
For such a non-login shell,
$HOME/.bashrc is sourced to allow
for other kinds of customization.
So your shell cutomization is generally split between .bash_profile, which sets environment variables, and .bashrc, which creates aliases, key-bindings, and other such things.
clean, which really is
just the command rm *~:
alias clean='rm *~' # cleanup all those emacs ~ filesor something like this:
alias untar='tar xvf' # now I never need to remember xvfAnother common use is to modify command behavior, like this:
alias rm='rm -i' # prompt before removing filesAliases are not inherited, so if you want to have them in every shell, you should put them in your
.bashrc
file.
set lists all the environment
variables and their values.
Perhaps the most important environment variable is
PATH. This is a list of directories, and when you
give a program name to the shell, it searches those
directories to find the program.
In our envrionment, /usr/local/skel/.global_bash
defines the default PATH variable, and some others as well.
So we source it to get those set. Below are
example .bash_profile and .bashrc files. Notice that the
environment variables are set in .bash_profile. That way
they're only set once, and their values are inherited otherwise.
Sample .bash_profile file |
########################################################### # Environment Variables ########################################################### # system default environment source /usr/local/newskel.2009/.global_bash # some personal settings export PS1="\h[\!][\w]$ " # Set prompt my way export EDITOR=/usr/local/bin/emacs # Default editor export HISTSIZE=100 # max number of events for current session export HISTFILESIZE=100 # max number of events accross sessions export PATH=$PATH:. # always make it last in path ... still a security flaw? source ~/.bashrc |
Sample .bashrc file |
###########################################################
# Aliases
###########################################################
alias cp='cp -i' # prompt before overwriting existing files
alias mv='mv -i' # Prompt before overwriting existing files
alias rm='rm -i' # prompt before removing files
alias cd..='cd ..' # takes care of that typical typo
alias EXIT='exit' # in case caps lock gets stuck
alias ls='ls -F' # let's get ls looking right!
alias clean='rm *~' # cleanup all those emacs ~ files
alias emacs='emacs -bg darkslategray -fg wheat -cr orchid -ms orchid'
alias l='ls -l'
alias h='history | tail'
alias xterm="xterm -cn -cu -j -sb -sl 1024 -vb -T ${USER}@${HOST} -n xterm@${HOST}"
###########################################################
# Misc
###########################################################
umask 066 # files are by default -rw------
ulimit -c 0 # doesn't allow those huge core files!
set -o ignoreeof # ^D will not exit shell
set -o noclobber # noclobber, redirection with ">" will not
# overwrite existing files, and ">>" needs the
# target ile to exist
|
.bash_profile and .bashrc don't
look exactly like this, but are similar. Look under Resources
for this course, and you'll see some other customization
suggestions.