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.

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

  • 2
    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. 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
    Apr 18, 2018 at 8:08
  • 1
    @BrianCannard it should be corrected as chmod +x /etc/profile.d/myscript.sh
    – prab4th
    May 13 at 1:21
  • I had this requirement and I ended up adding the script to /etc/profile.d directory
    – Cris
    May 23 at 22:41

/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.


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
  • 1
    This execute on bash initialize, not in user login, by example using gdm.
    – e-info128
    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.


Just add the bash script to the Startup applications.

In the command field, enter

bash /full/path/to/bash/script.sh/
  • 4
    How do you add "bash /.../script.sh" to the Startup applications from the command line?
    – aleb
    Feb 1, 2013 at 20:02
  • 2
    OP never mentioned a GUI; what if they're on a remote server via SSH and don't have a GUI? None of the desktop tags were applied to the question...
    – Doktor J
    Jul 10, 2019 at 20:48
  • 1
    The title specifically mentions at login and nothing about desktop GUIs.
    – RichieHH
    Oct 21, 2019 at 8:25
  • Adding a bash command does not resolve the issue, the OP need to run after/on a user login, so all the startup scripts like init scripts or rc.local came before that. The top answer here solved my issue of running a reverse ssh tunneling at startup and launch a vnc server that needed to run after a user login.
    – charles
    Jan 7, 2021 at 20:05
  • Besides all the other issues with this answer, the trailing slash is also wrong.
    – Marki
    Feb 15 at 23:38

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.