I need to run a script when I login and logout in my Ubuntu.

I tried to put the script in my ~/.bash_login but it didn't work.

Is there a better location where I can run my script?

My script is located in /home/gsd/script/login.sh and it's executable.


my script runs when i type: /home/gsd/script/login.sh and it set with +x

now, i only have: touch /home/gsd/test.txt in the ~/.bash_login to test.

the file test.txt is never created

edit 2:

gsd@laptop:~$ ll ~/.bash*
-rw------- 1 gsd gsd 38639 2012-01-25 17:25 .bash_history
-rw-r--r-- 1 gsd gsd    29 2012-01-25 15:22 .bash_login
-rw-r--r-- 1 gsd gsd   220 2011-11-03 19:22 .bash_logout
-rw-r--r-- 1 gsd gsd  3136 2011-11-04 08:00 .bashrc
  • does .bash_login has the right permissions? what does ls -l .bash_login returns? Jan 25, 2012 at 14:48
  • Do you also have a ~/.bash_profile?
    – enzotib
    Jan 25, 2012 at 14:52
  • -rw-r--r-- 1 gsd gsd 313 2012-01-22 11:56 .bash_login Jan 25, 2012 at 15:36
  • 1
    chmod +x .bash_login
    – Zoke
    Jan 25, 2012 at 16:59
  • 1
    It is not necessary to chmod +x .bash_login (set the executable bit on .bash_login). The Bash manual is a bit confusing in this area, but Bash does not eXecute .bash_login like a shell script. It does read the file and then executes the commands within it (You can do something similar by running source ~/.bash_login). Jan 25, 2012 at 19:31

4 Answers 4


If .bash_profile exists, then Bash will not read .bash_login (or .profile). This annoying feature is described in some versions of the Bash manual, but not all.

.bash_profile and .bash_loginare analogous, so I recommend you put your commands in .bash_profile, because it's is commonly used and .bash_login is relatively unknown. Also consider putting your commands in .bashrc instead of .bash_profile. The manual describes difference between "interactive non-login shell" and "interactive login shell", so be sure to read that section.

The GNU Bash Reference Manual version 4.1: Bash Startup Files says:

looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

Also see this question on superuser, and this Bash howto (Don't be deterred by the freeunix.dyndns.org:8088 address--- it's still a good quality manual and I have used it for years).

Update, since you say you don't have a .bash_profile.

It sounds like you are not using what's called a "interactive non-login shell" (See the Bash manual for a detailed description).

To test this, add something like the following each file: .bashrc, .bash_profile and .bash_login.

echo "DEBUG: I am .bashrc"

echo "DEBUG: I am .bash_profile"

Then log out and log in again. When you log in, I bet you will only see the phrase "DEBUG: I am .bashrc" but not "I am .bash_profile". If so, it means you are a "interactive non-login shell", which simply means that Bash will call .bashrc but not .bash_profile. For information why these dotfiles are the way they are, see @Andrejs Cainikovs's post below and http://mywiki.wooledge.org/DotFiles

  • i updated my question, i don't have a bash_profile Jan 26, 2012 at 13:15
  • Ok, because earlier you said "yes i have a bash profile". Jan 26, 2012 at 18:07
  • yes, i had it then removed it but same results Feb 11, 2012 at 16:57
  • 5
    This answer solves the problem, I think it's worth accepting it. Mar 26, 2012 at 17:23
  • 1
    Also note that these bash files are only run when logging into a shell (through Terminal, ssh, etc.), not when logging into the GUI!
    – mivk
    Oct 18, 2012 at 8:19

Login shells, regardless of if they are interactive or non-interactive read and execute the .profile

Interactive shells read and execute .bashrc.

Often you will see that /etc/profile sources .bashrc - thus all settings made in .bashrc will also take effect in a login shell regardless of whether it is interactive or non-interactive.

The order of execution of the initialization scripts for a shell is dependent on if the shell is interactive or non-interactive and not related to if it is a login script or not.

When bash is invoked as an interactive login shell it reads and executes commands from the /etc/profile. Then Bash will then try in order to execute ONLY the first file exists and is readable of the following:

  1. .bash_profile
  2. .bash_login
  3. .profile

If one of these files is found but can not be read, it will cause an error. There is no error if any are NOT found.

This same process is followed when a non-interactive login shell is invoked with the --login option.

  • 1
    AFAIK, .profile file is read and executed only when the user logs in using the graphical interface. Otherwise it is not read. Jul 5, 2017 at 21:05
  • @RajaRaviVarma I just tested (Ubuntu 16.04) and ~/.profile definitely is read in non-graphical login shells (I tested by switching to another terminal via Ctrl+Alt+F2 and logging in there).
    – waldyrious
    Nov 7, 2017 at 18:58
  • I agree with @RajaRaviVarma - I'm logging into an Ubuntu 16.04 VM over SSH and .profile is not read, only .bashrc
    – jamesc
    Jul 13, 2018 at 12:28

Bash only looks for .bash_login or .profile files if it is executed as interactive login shell. When it is executed as interactive non-login shell it reads .bashrc.
Commonly it is the second case, i.e when you run gnome-terminal bash is run as non-login shell.

This clearly states that if you are booting into Gnome, .bash_login will not be executed. But if you lower runlevel to boot directly to bash, the same file will be executed upon succesfull login.
I assume .bash_login will be executed in case of remote SSH connections as well.

Partially ripped from here.


Make sure the script you want to run is executable. Run chmod +x scriptname to do that. Also make sure the script starts with the correct shebang (#!/bin/bash for shell scripts). Lastly, use all full paths when executing it, e.g., /usr/bin/echo instead of echo just incase the environment that your script will execute in is not identical to the one you are used to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.