Shell - (Startup|Login) script (.bash_profile, .profile, .login)

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

The shell login scripts are the script that launch automatically after a login shell and permit to store permanently the values of environment variables.

Advertising

3 - Management

3.1 - Login Script By Shell

You can found them in two directory:

  • in the home user (~) for a user scope
  • in the /etc/ directory for a system scope. The /etc/ files contains system wide environment stuff and startup programs. All customization that you put in this file will apply for the entire environment variable on your system.

The login script mechanisms depends of the shell type.

Scope Shell Script or directory to modify
User Bash ~/.bash_profile
User Bourne or Korn shell ~/.profile
User C shell (csh) ~/.login
Global (All users except root) Bash /etc/profile.d
root Bash /root/.bash_profile
Advertising

3.1.1 - /etc/profile.d

Bash - The /etc/profile is the main entry point and call all custom script in the directory (/etc/profile.d). Add your global configuration in this directory. Example:

/etc/profile.d/java.sh
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")

3.2 - How changes can take effect immediately?

You can then make the changes to .bash_profile take effect immediately by typing the following command:

source .bash_profile
  • By adding paths to your .bash_profile, you can place utilities and programs in your path and be able to execute them without having to type ./ in front of the command.
  • the source command and the point '.' are similar:
source ./.bash_profile

is the same as

. ./.bash_profile

3.3 - How to edit it ?

To edit the bash_profile :

  • change to your home directory (ie: /home/myname)
  • edit the file. Example with gedit.
gedit .bash_profile
PATH=$PATH:$HOME/bin:/usr/kerberos/sbin:/usr/local/sbin:/sbin:/usr/sbin:/root/bin:

Note the fullstop before 'bash'. This '.' makes it a hidden file

Advertising