78

After some search about it I created a ~/.hushlogin file and it worked, but only for new windows. Is there a way to make it work for new tabs too?

7 Answers 7

138

On Mavericks, creating the empty file ~/.hushlogin removes the line “Last login”, for new windows and new tabs.

On my Mac it works.

8
  • 33
    The problem is that when opening a new tab with "Same Working Directory" enabled, the current directory is searched for .hushlogin. Unless you put a .hushlogin in every single directory, it will only see ~/.hushlogin if you open a new tab when you're in ~. This is in direct conflict with the feature of preserving the current working directory.
    – davidA
    Dec 16, 2013 at 4:53
  • 3
    @gibatronic — It works always for a new window. It works for a new tab if I am in ~. Alas, it does not work for a new tab if I am not in ~. Dec 16, 2013 at 7:36
  • yep... that's why I ended up doing what int3h said in the accepted answer
    – gibatronic
    Dec 27, 2013 at 21:37
  • 12
    It does not work under Yosemite (for me). Open a new tab in a folder different than ~ and you will see that. May 12, 2015 at 4:39
  • 1
    Works perfect for MacOS Sierra. Dec 27, 2016 at 19:53
62

Solution

This is running OS X 10.8.3. I haven't tested it on other versions, but so long as Terminal has the above option, then it should work.

  1. In Terminal.app, go to Preferences->Settings and select the profile you're using. Go to the 'Shell' tab and under the 'Startup' heading, check 'Run command:' and enter into the box:

    login -fpql your-username /bin/bash
    

    Substitute your-username with your actual Unix username. If you use a shell other than the default bash shell, replace /bin/bash with the full path to that shell (yes, even if you've already set it in Preferences->Startup.)

  2. Make sure 'Run inside shell' is unchecked.

  3. If you have the "Prompt before closing: Only if there are processes other than login shell and:" option selected, add "login" and "bash" to the list of processes it will not prompt for.

  4. Make sure you have a ~/.bashrc file, since this will be the file bash uses on startup from now on rather than ~/.bash_profile. I just have one file reference the other using this method. You also need to be sure it sources /etc/profile.

Explanation

We want to run login with the -q option to tell it to supress the "Last login" message, even in the absence of a .hushlogin file. (As noted above, login will only look in cwd for that file, not your home directory, so you'd need a .hushlogin file in every directory you'd open a shell to, for that method to work.)

The problem is Terminal runs something like login -pfl your-username /bin/bash -c exec -la bash /usr/local/bin/bash when you create a new shell (I'm using homebrew's version of bash, hence the weird bash path at the end,) which lacks the -q flag.

Unfortunately, there's no way to directly change the arguments Terminal uses, so we just trampoline a new login session with login -pfql from Terminal's default login -pfl session. Inelegant, but it works.

We need to have the -q option and the path to bash to keep the "New windows/tabs open with: Same Working Directory" option working. If you don't care about that option, you can remove that flag and argument, and probably avoid the .bashrc stuff above.

14
  • 1
    I don't want to abuse, but is there any way to keep the same working directory on new tabs?
    – gibatronic
    Apr 25, 2013 at 17:21
  • 2
    nope... always opens my home dir, if I rollback those changes or use another profile that works fine.
    – gibatronic
    Apr 29, 2013 at 13:11
  • 6
    In your ~/.bashrc, add a line [[ -f /etc/profile ]] && . /etc/profile at the very top. You may also want to add something like \[\e]0;\w\a\] to your $PS1 variable, too.
    – int3h
    Apr 30, 2013 at 18:09
  • 5
    Had a problem with $PATH not including some paths, but was cleanly fixed using @int3h method. Thanks!
    – inket
    Apr 19, 2014 at 23:42
  • 4
    What worked best for me is login -fpql nhooyr /bin/zsh -l. The -l makes sure the shell is started as a login shell so you don't need to source anything yourself.
    – nhooyr
    May 26, 2020 at 14:56
17

you could just add a clear to your .bash_profile

1
  • 2
    it's a good alternative, but I don't like how clear just prints new lines to "clear" the screen.
    – gibatronic
    Aug 28, 2014 at 15:13
17

Adding ~/.hushlogin is fine unless you want to open a new tab in the same folder, or open Terminal from Finder on the exact folder, in that case it won't work.

Changing a running command to another login is something I would like to avoid because of the strange unnecessary scheme login -> login -> zsh. You can see it in Activity Monitor, but also it will show up when you are quitting interactive programs (like, python repl) in the message that python, login and zsh are running.

Putting clear in ~/.zshrc is not ideal since on mac it just prints a lot of newlines (and if you scroll back, you'll see them).

The best way that I found up to this point is adding printf '\33c\e[3J' to ~/.zshrc (or in Terminal/Preferences/Profiles/Shell/Startup/Run command with Run inside shell checked). I chose beginning of ~/.zshrc file since startup command is running after it and if the ~/.zshrc file is heavy you can briefly see Last Login message before printf is executed.

3
  • 6
    This should be the accepted answer in 2022. Mar 12, 2022 at 19:28
  • 1
    I still see a flash of the logged history message (and a message from my company about the computer being their property) for a split second, but then it disappears and looks fine. It's too bad there's no way to make it treat it as if there was a .hushlogin file in every directory.
    – n8jadams
    May 18, 2022 at 21:08
  • If using iTerm2, you can add this command to your terminal profile under the general tab > Send text at start. Jan 18 at 3:37
7

This might be OS version dependent. On Terminal 2.3 (on 10.8), touching the file ~/.hushlogin suppresses the 'last login' message for new tabs as well as new windows. That is, it Works For Me.

Just in case it helps to work out what's going on (and in case you don't know), note that the 'last login' message is in principle coming from login(1), and not the shell. Or, more precisely, if a shell is invoked in a particular way (including starting it with the -l option), then bash will "act as if it had been invoked as a login shell" (zsh may have a similar feature, though I can't find it right now). Now, it could be that when Terminanl opens up a new tab in your OS X version, the shell is effectively simulating opening a login shell, and maybe getting this detail wrong. But if you have the 10.8 version of bash/zsh (namely 3.2.48 / 4.3.11), then I don't know what might be amiss.

10
  • 1
    that's a good theory, indeed my bash is 3.2.48 (OS X 10.8.3). thanks for your time! but it was a more naïve issue... I just needed to restart my computer!
    – gibatronic
    Apr 2, 2013 at 22:15
  • 1
    That's odd (I wouldn't have thought a restart would have had any effect). Have a look at the output of ps aux|grep yourusername|grep -v / (replacing yourusername obviously!). I see a number of lines login -pf myusername each with a matching -zsh process. The - preceding the zsh is indicating that the shell is being invoked as a login shell (zsh is the shell I use, but you should see something very similar if you use bash). If the zsh/bash lines don't have a leading -, then they're not (acting as) login shells. This should just work – I'm rather perplexed. Apr 3, 2013 at 16:08
  • 1
    running that command I got one login -pf and two -bash processes
    – gibatronic
    Apr 3, 2013 at 17:02
  • 1
    uhmm... I found what seems to be the problem, whenever I open a new tab from a different directory than my home folder ~ I get the login message, it seems that the terminal always look for .hushlogin on the current directory! that's why it only works for new windows for me, new windows always open at ~!
    – gibatronic
    Apr 3, 2013 at 19:07
  • 2
    Aha: that sounds plausible. Check your Terminal preferences, then – there's a setting 'New tabs open with...', and there you can set 'Default working directory'. Apr 3, 2013 at 19:52
1

This is an old question, but I don't think ones thing has been resolved in other answers. If you want to not show the login but want to have a welcome message from /etc/motd appear, then you can do one of the following.

In the first method start by

cd ~
touch .hushlogin

Then you can add cat /etc/motd to the top of either ~/.zshrc or ~/.bashrc depending on which shell you use.

The second approach is to add

clear
cat /etc/motd

to the first two lines of either ~/.zshrc or ~/.bashrc depending on which shell you use.

0

A simple solution without changing anything related to login would be just to add the clear command in the end of your ~/.bashrc or ~/.zshrc file. It will clear the terminal in initialization from any initialization prints. It works for me very well.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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