Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed 

Tweak your BASH startup files

Depending on whether the BASH shell is started as a login shell or a non-login shell, different BASH startup files are going to be run (see the references below and the Invocation section of man bash for more detailed information). To make sure your customized BASH shell works the way it should you can source /private/etc/bashrc from /private/etc/profile and ~/.bashrc from ~/.bash_login (or alternatives):


# BASH startup files

/private/etc/profile      # login shell
~/.bash_profile
~/.bash_login
~/.profile

/private/etc/bashrc      # non-login shell
~/.bashrc


# list your BASH startup files
ls -a ~ | grep \.bash
ls /private/etc | grep -E "bash|profile"

# backup the existing BASH startup files
sudo cp -p /private/etc/profile /private/etc/profile.orig
sudo cp -p /private/etc/bashrc /private/etc/bashrc.orig
cp -p ~/.bash_profile ~/.bash_profile.orig
cp -p ~/.bash_login ~/.bash_login.orig
cp -p ~/.profile ~/.profile.orig
cp -p ~/.bashrc ~/.bashrc.orig


# /private/etc/profile should contain: 
# if [[ -f /private/etc/bashrc ]]; then source /private/etc/bashrc; fi 

sudo sh -c 'echo "if [[ -f /private/etc/bashrc ]]; then source /private/etc/bashrc; fi" >> /private/etc/profile'
sudo nano /private/etc/profile


# ~/.bash_profile or ~/.bash_login or ~/.profile should contain: 
# if [[ -f ~/.bashrc ]]; then source ~/.bashrc; fi 

echo 'if [[ -f ~/.bashrc ]]; then source ~/.bashrc; fi' >> ~/.bash_login
nano ~/.bash_login


# then add a simple 'echo' command to the existing BASH startup files
sudo sh -c 'echo "echo \"... running /private/etc/profile ...\"" >> /private/etc/profile'
sudo sh -c 'echo "echo \"... running /private/etc/bashrc ...\"" >> /private/etc/bashrc'
echo 'echo "... running ~/.bash_profile ..."' >> ~/.bash_profile
echo 'echo "... running ~/.bash_login ..."' >> ~/.bash_login
echo 'echo "... running ~/.profile ..."' >> ~/.profile
echo 'echo "... running ~/.bashrc ..."' >> ~/.bashrc

# check added lines
sudo nano /private/etc/profile
sudo nano /private/etc/bashrc
nano ~/.bash_profile
nano ~/.bash_login
nano ~/.profile
nano ~/.bashrc

exit

# now relaunch Terminal.app and play around with ...
bash
echo $SHLVL
exec bash
echo $SHLVL
exec bash -$-
exec env -i /bin/bash
exec bash --login
exec bash -l
echo $0
echo $BASH_ENV
exec bash -norc
bash
bash --init-file file
bash --rcfile file
open-x11 xterm
ssh some_other_user@internal_IP_address     # cf. http://textsnippets.com/posts/show/1326
...


Further information:

- Controlling Bash At Startup
- Make X11 Xterm Launch Login Shells
- Initialisation Scripts
- Difference between .bashrc and .bash_profile
- Fun and profit by modifying your Bash startup files in OS X, Linux, and other fine unices
- bash shell aliases
- A Sample .bashrc File
- Theming Bash
- .bash_profile
- .bashrc

Pretty PS1 Prompt for Bash

This is just a simple colored bash prompt which differentiates nicely between normal and root users.

To use this put it in ~/.bashrc, or for it to apply systemwide, in /etc/profile

if [ "$TERM" != 'dumb' ] && [ -n "$BASH" ] && [ -n "$PS1" ]
then
        if [ `/usr/bin/whoami` = 'root' ]
        then
                export PS1='\[\033[01;31m\]\h \[\033[01;34m\]\W \$ \[\033[00m\]'
        else
                export PS1='\[\033[01;32m\]\u@\h \[\033[01;34m\]\W \$ \[\033[00m\]'
        fi
fi

Updated: It's now more concise and functionally more sensible.
« Newer Snippets
Older Snippets »
2 total  XML / RSS feed