Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Friday, May 2, 2014

Unix Startup Scripts

There is always a confusion on which dot file to use to initialise environment variables under unix (i.e) .bash_profile , /etc/profile , .bashrc, .bash_login et.al?

There is two different types of shells called login shell & interactive shell and each has it own sequence of dot files to look for while starting. How do you differentiate them ? When trying to remotely login through ssh or any shell which asks to login is considered as login shell.



In the case of a login shell, the /etc/profile file is sourced first. Then it looks for the files in below sequence
.bash_profile -> ~/.bash_login -> .profile and whichever is found first & readable is executed.

In the case of a interactive shell, then it looks for ~/.bashrc and executes it if present.

On my setup  what ideally I do is to keep my environment variables in ~./profile file as the subshell (or) subprocess would inherit that. All the alias definitions would go into the ~/.bashrc file.

A good reference is available here


bash vs sh

There is always a confusion as to why we define the following while writing a shell script in unix based system:
#! /bin/sh (or) #! /bin/bash
#! is known as shebang which lets the rest of the line to be considered as a interpreter directive by the program loader. Thus the program loader knows which interpreter needs to be used to execute the shell script.
sh - Bourne Shell used to be the default shell of the old unix systems.
bash - Bourne-again shell is the outcome of the GNU project and it supersedes sh. In Mac and most of the linux distributions bash is shipped as the default interpreter. In fact sh is considered as a system shell and hence in most distributions is mostly a symbolic link to bash.