I am trying to write bash shell script in Ubuntu 11.10 Linux distro, that will get executed automatically on logging into the system. But I am not able to figure out that what to write in script that by it will get automatically executed on logging in.

  • The solution would vary depending on what "log in" actually means. If it means "starting a login shell (in a terminal, for example)", then most of the answers below would help, but if you mean that the script should run as soon as you log into a graphical desktop environment, without ever starting a terminal or shell session, then you may want to clarify this in the question.
    – Kusalananda
    Commented Apr 19, 2023 at 4:19

5 Answers 5


If you want it to be global, modify


or add a script to


If you want it to be user-specific, modify

  • 3
    Don't forget that the extension should be .sh if you put your script inside of /etc/profile.d/. chmode +x /etc/profile.d/myscript.sh of course too. Commented Aug 4, 2017 at 22:59
  • 1
    Raspbian Stretch. Trying to run /usr/local/bin/pihole -c command on autologin of specific user. Added this line to /home/$USER/.profile - nothing happens. When I add script to /etc/profile.d then it works, but it's global and runs it even when I open ssh session(which is not what I want). Adding to .bashrc works too, but runs any time I open new shell (which is no what I want). Question: Why adding to /home/$USER/.profile wouldn't work???
    – Drew
    Commented Apr 18, 2018 at 8:08
  • 1
    @BrianCannard it should be corrected as chmod +x /etc/profile.d/myscript.sh
    – prab4th
    Commented May 13, 2022 at 1:21
  • I had this requirement and I ended up adding the script to /etc/profile.d directory
    – Cris
    Commented May 23, 2022 at 22:41
  • This would run the script every time the user starts a login shell, regardless of whether they have just logged in or not. It is unclear whether it would run when the user logs in (e.g. via a graphical display manager; it would depend on the setup of their system).
    – Kusalananda
    Commented Apr 19, 2023 at 4:23

/etc/profile or $HOME/.profile or $HOME/.bash_profile

I would highly recommend against using /etc/profile.d/yourscript.sh if it produces output. When you use a non-interactive session, you will receive a $TERM is not set message. This is noticeable when using the ssh protocol, like scp. Usually not a big deal, however, Veeam doesn't like it and will throw a warning. I know Veeam is not the topic here, but it's worth mentioning that not all applications will gracefully ignore the $TERM is not set warning.

In short, if the script generates output, place it in the locations specified on the first line. However, if you're modifying the environment and your script doesn't generate output, then use the latter.

  • Whether or not this would be executed when the user logs in depends on whether the user starts a login shell or not (which they might not do if they log in via a graphical display manager). It is unclear from the question what the user means by "logging in". In any case, your suggestion would mean the script is run each time the user starts a login script, not just when logging in.
    – Kusalananda
    Commented Apr 19, 2023 at 4:26

If you want to be more bash specific you can also write you code in ~/.bash_profile or ~/.bash_login.

And you can source any script therein for example:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
  • 3
    This execute on bash initialize, not in user login, by example using gdm.
    – e-info128
    Commented May 30, 2020 at 0:31

Just a data point since the question is tagged Ubuntu.

Under Ubuntu 20.04 and 22.04, the only option to autoload of a script/command at each login is (that worked for me) to add it in


As stated in the comments, adding it to ~/.profile doesn't seem to invoke the script. Adding it to /etc/profile.d/ is not per login.


To execute a shell script on login in Ubuntu 11.10, you can add your script to the ~/.bashrc file. This file is executed each time a login shell starts up, so any commands or scripts added here will be run whenever you log in to your system. To add your script to ~/.bashrc, Open a terminal and navigate to your home directory by typing: cd ~/ Next, open the .bashrc file in a text editor using the following command: nano .bashrc Once you have the file open, navigate to the end of the file and add the following line: /path/to/your/script.sh Make sure to replace "/path/to/your/script.sh" with the actual path to your script. Also, make sure that your script is executable by running: chmod +x /path/to/your/script.sh Save and close the file, and the next time you log in, your script should be executed automatically. Let me know if it works for you!

  • 2
    The ~/.bashrc file is sourced each time an interactive shell is started, no matter if the user has just logged in or not.
    – Kusalananda
    Commented Apr 19, 2023 at 4:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .