Remove "Last login" message for new tabs in terminal - macos

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?

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

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.
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.)
Make sure 'Run inside shell' is unchecked.
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.
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.

you could just add a clear to your .bash_profile

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.

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.

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.

On my MacOS Big Sur 11.1 it works.

Related

permanently source in terminal (Mac OS)

this is probably a very basic question. I am using the bash shell still (Catalina). And I downloaded gromacs. Every time I open a new terminal window I first have to put
source /usr/local/gromacs/bin/GMXRC
in first, otherwise I will just get command not found.
How can I permanently source this so that I don't have to reenter it all the time?
I tried editing the /etc/paths file but that didn't work.
If you are still using the bash shell on macOS, all you have to do is:
Put the line:
source /usr/local/gromacs/bin/GMXRC
at the end in .bash_profile.
.bash_profile is in home directory, so you can open it like:
open ~/.bash_profile
Why .bash_profile?
.bash_profile is executed whenever a login interactive shell starts and on mac, every interactive shell is a login shell by default.
If in case you switch to zsh, instead of .bash_profile, I think .zlogin should be used.
But that would depend entirely on your need. Read about zsh configuration files in case you ever need that.

Zsh show fail every time when I open my terminal

I'm using a Mac with OS X Yosemite and Zsh.
By accident,I delete the content of three files below:
.bashrc
.bash_profile
.profile
After that ,when I open my terminal.
The Zsh will show fail under the last login information,it confused me ,and I want to know why.
You might want to look at a duplicate question: Zshell starts up with exit status of 1 after uninstalling RVM
It has an answer that solved the issue for me:
I found a .zlogin file on my system that contained some rvm-related code. I've deleted the code, and the problem is solved!
Zsh (by default) doesn't read from .bashrc, .bash_profile, or .profile, so the contents of these files shouldn't matter. You also didn't mention which .bashrc, .bash_profile, and .profile were erased… These files exist in both your /Users/username directory and /etc. The files sourced by zsh at startup are listed in the OS X zsh man page (man zsh in a terminal) under "STARTUP/SHUTDOWN FILES". The only reason it would call one of the previously mentioned files is if they were explicitly sourced in one of the default files.
My suggestions:
Check the contents of /etc/zshenv (this is the only zsh-specific file in my etc directory). Mine has only the following:
# system-wide environment settings for zsh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
Can you log in at all using zsh? If not, can you log in using another shell? You can do this in the OS X Terminal.app by going to Preferences -> General and changing the option for "Shells open with:" from "Default login shell" to Command (fill in another shell, i.e., /bin/bash or /bin/sh). If you can log in with any shell, try the following solution from this question:
Looking for the error
All shell output goes to the terminal, so you could just redirect it
when starting it. As you are looking for error messages during
initialisation, I'd suggest the following procedure:
Disable the problematic configurations
Open a terminal
Check the value of SHLVL: echo $SHLVL
Re-enable the configurations
Start a new z-shell from within the running shell with zsh 2> zsh-error.log, this redirects stderr to the file 'zsh-error.log'.
Check the value of SHLVL again. If it is bigger then previous value then exit the current shell (exit). (Explanation below)
Have a look at 'zsh-error.log' in the current directory.
If 'zsh-error.log' does not show anything, you may want to run zsh -x
2> zsh-error.log in step 5 instead. This provides a complete debug
output of anything zsh does. This can get quite huge.
As the answer suggests, those logs can get enormous if you are sourcing man files at startup. Just a bare shell should result in a reasonably small log file.
Finally, you can retrieve a list of all the files sourced by zsh on startup by running zsh -o sourcetrace.
Hope this helps.

How to run ~/.bash_profile in mac terminal

So I'm installing some things for coding and personal usage, and I need to run this in the terminal (I'm on Mac if you didn't read the title).
~/.bash_profile
It just says permission denied, Im running OSX 10.8.4 Mountain Lion. How do I bypass this?
On MacOS: add source ~/.bash_profile to the end of ~/.zshrc.
Then this profile will be in effect when you open zsh.
You would never want to run that, but you may want to source it.
. ~/.bash_profile
source ~/.bash_profile
both should work. But this is an odd request, because that file should be sourced automatically when you start bash, unless you're explicitly starting it non-interactively. From the man page:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it 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. The --noprofile option may be used when the shell is started to inhibit this behavior.
If you change .bash_profile, it only applies to new Terminal sessions.
To apply it to an existing session, run source ~/.bash_profile. You can run any Bash script this way - think of executing source as the same as typing commands in the Terminal window (from the specified script).
More info: How to reload .bash_profile from the command line?
Bonus: You can make environment variables available to OSX applications - not just the current Bash session but apps like Visual Studio Code or IntelliJ - using launchctl setenv GOPATH "${GOPATH:-}"
As #kojiro said, you don't want to "run" this file. Source it as he says. It should get "sourced" at startup. Sourcing just means running every line in the file, including the one you want to get run. If you want to make sure a folder is in a certain path environment variable (as it seems you want from one of your comments on another solution), execute
$ echo $PATH
At the command line. If you want to check that your ~/.bash_profile is being sourced, either at startup as it should be, or when you source it manually, enter the following line into your ~/.bash_profile file:
$ echo "Hello I'm running stuff in the ~/.bash_profile!"
No need to start, it would automatically executed while you startup your mac terminal / bash. Whenever you do a change, you may need to restart the terminal.
~ is the default path for .bash_profile
I was getting this error on zsh(mac os Big Sur 11.3), This is how i solved this :-
Go to Terminal.
cd /users/<yourusername>
Once you reach here issue a command :
ls -al
You will see a lot of files and one specific file .zprofile. This is your user profile. We need to edit this.
After this we need to edit the file. Issue the below command :
nano .zprofile
Once you issue this command file will be opened for edit. Add the path details for maven.
M2_PATH="/Users//code/apache-maven-3.8.1/bin" //add your path of maven diretory
PATH="${PATH}:${M2_PATH}"
export PATH
press ctrl + X and save the file.
Issue command after saving the file :
source .zprofile
Once done, you will be able to run the mvn command.
If the problem is that you are not seeing your changes to the file take effect, just open a new terminal window, and it will be "sourced". You will be able to use the proper PATH etc with each subsequent terminal window.

Why do I need to source bash_profile every time

I have installed Hadoop and every time I want to run it, first I have to do this:
source ~/.bash_profile
or it won't recognize the command hadoop
Why is that?
I am on OSX 10.8
Now that we've narrowed down the problem:
Run ps -p $$ at the command line to check if you are, in fact, using a bash shell.
Realize that you are in zsh, which means you should be editing your profile in .zshrc.
Copy the offending lines from .bash_profile to .zshrc, OR
Modify your .zshrc to directly source your .bash_profile.
UPDATE: Do what #TC1 mentions in the comments and keep the shell-specific code in each shell's own profile, and from those profiles, only source shell-agnostic code.
On Mac Catalina, I just had to open "preferences" on terminal and change the "shells open with" from "default" to "Command(complete path)", which the default path was "/bin/zsh". touch ~/.zshrc, if that file doesn't exist already, and copy/paste your stuff from ".bash_profile" into the ".zshrc" file.
To elaborate, with terminal running, I opened "settings" from the Terminal menu on the Mac navbar. On the "General" tab, look for "Shells open with" select "Command (complete path)", and type in /bin/zsh.
bash_profile.sh is applicable for bash shell.
if your default shell is not bash and if your default shell is someother shell for example zsh then you have to manually load the .bash_profile using source ~/.bash_profile.
You can always change the default shell to bash shell so that the .bash_profile file will be automatically loaded.
Inorder to automatically load .bash_profile, you can update your default shell to bash using the command chsh -s /bin/bash
cat /etc/shells will list the default shells available in the
machine
echo $SHELL will display the currently active shell in your machine
To change active shell to a different shell, use chsh -s /bin/bash.
Then echo $SHELL to verify if the shell has changed.
Terminal -> Preference -> profile -> Shell -> Run command : source ~/.bash_profile
Tick on run inside shell.
After doing all those , just logout and check weather everything works fine or not
I tried the approved answer. Changing the .zshrc file works for one of my machines. But for the other one, when I run ps -p $$, it is -sh under the command. And I changed both bash and zsh files, neither of them works for me this time.
So I found this
https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
it mentioned
"When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. "
so I went to that file /etc/profile and add "source ~/.bashrc" in that file. Then it works since every time a terminal is opened, it runs the command in that /etc/profile file.
Not sure if this is the best solution but it works.
sudo nano /etc/bashrc and change that, restarted the terminal and it finally remembered with command. Tried ~/.bash_profile and ~/.bashrc without success, just wasn't sourcing it.
Go to “Preferences/Profiles then look in the right window and find “shell”.
Once in that if your “Startup Run Command” hasn’t been turned on. Click the box to turn it on and in the command section type:
(If you made a .zsh file)
source .zsh ; clear
(If you made a .bash_profile)
source .bash_profile ; clear
Doing this ; clear
Will clear your terminal to a new page so that you don’t see your terminal display:
“Last login: etc
User#user-Mac ~ % source .zsh
If you typed the commands as I said you should just get this:
User#user-Mac ~ %
That way you will be greeted with a clear page with no extra jumbo. Also to make sure that your .zsh or .bash_profile aliases work type the following command to see a list of your custom aliases:
Alias
One alias I like to do is
alias LL=“ls -la”
This will display a tree or the directory you are in as well as hidden files.

In Bash, how can I tell if I am currently in a terminal

I want to create my own personal logfile that logs not only when I log in and out, but also when I lock/unlock my screen. Kindof like /var/log/wtmp on steroids.
To do this, I decided to run a script when I log into Ubuntu that runs in the background until I quit. My plan to do this is to add the script to .bashrc, using ./startlogging.sh & and in the script I will use trap to catch signals. That's great, except .bashrc gets run every time I open a new terminal, which is not what I want for the logger.
Is there a way to tell in Bash that the current login is a gnome login? Alternatively, is there some sort of .gnomerc I can use to run my script?
Edit: Here is my script:
Edit 2: Removed the script, since it's not related to the question. I will repost my other question, rather than repurpose this one.
Are you looking for a way to detect what type of terminal it is?
Try:
echo $TERM
From Wikipedia:
TERM (Unix-like) - specifies the type of computer terminal or terminal
emulator being used (e.g., vt100 or dumb).
See also: List of Terminal Emulators
for bash use : ~/.bash_logout
that will get executed when you logout, which sounds like what you are trying to do.
Well, for just bash, what you want are .bash_login/.bash_logout in your home directory (rather than .bashrc) These are run whenever a LOGIN shell starts/finishes, which happens any time you log in to a shell (on a tty or console, or via ssh or other network login). These are NOT run for bash processes created to run in terminal windows that you create (as those are not login shells) so won't get run any time you open a new terminal.
The problem is that if you log in with some mechanism that does not involve a terminal (such as gdm running on the console to start a gnome or kde or unity session), then there's no login shell so .bash_login/logout never get run. For that case, the easiest is probably to put something in your .xsessionrc, which will get run every time you start an X session (which happens for any of those GUI environments, regardless of which one you run). Unfortunately, there's no standard script that runs when an X session finishes.

Resources