Restart terminal without closing on MacOS - bash

How to restart my current MacOS terminal session without closing the window?
In Linux I use exec bash but it does not work in this environment. I made a few changes to the .bash_profile (prompt, alias etc) I would like to see without closing it and opening again.

Just type in the command:
exec bash -l
I guess that should do it.
For zsh,
exec zsh -l
This is needed because every shell on macOS by default is a login shell.
Justing writing exec bash would replace the current shell with a non-login shell which is not the same effect as closing and re-opening the terminal.
exec would make new bash -l process replace the current shell. If exec is not used, bash -l would spawn a new shell over the current shell incrementing the $SHLVL.

For me none of the other solutions work for ZSH.
Simply source ~/.zshrc did the job actually.
Note: running exec zsh -l outputs /Users/my_username/.zprofile:3: command not found: yarn (where my_username is my username). But running only the command mentioned above does the job.

If your session is hanging (maybe your SSH connection was interrupted), you won't be able to restart by entering a command.
On iTerm, you can navigate to "Session" > "Restart Session" in the menu bar.
You can also add a key binding for this via "iTerm" > "Preferences" > "Keys" > "Key Bindings" > "+".
Keyboard Shortcut: Your choice, I use Cmd-R
Action: "Select Menu Item..." > "Restart Session"

The actual answer, assuming you interpret the question as having the same effect at the state of the terminal session as closing and reopening Terminal would, appears to be to run the executable of the used shell to start a new session:
https://unix.stackexchange.com/a/217907/137983
zsh
If you're not on Catalina where ZSH is the default shell, it's going to be:
bash
After this, all state of the previous session (like session environment variables) will be reset. Also ZSH profile should be re-sourced I think.

If you've made any changes to your .bashrc and .bash_profile, then without closing the terminal you can specify alias in your .bashrc and .bash_profile as shown below to restart the terminal:
alias rest='exec bash -l;source ~/.bashrc;source ~/.bash_profile'
This command sources the .bashrc and .bash_profile again, in the sense restarts the terminal and creates a new terminal session. It works for me. Give this a try!
So, if you wanna restart the terminal, just enter rest (short for restart) in your terminal.

Related

MacOS(Catalina) terminal bash looks weird

enter image description here
I just have installed Catalina on my imac, and I saw a terminal bash looks weird.
It should be ~$ but my terminal shows ~%
Can anyone change this uncommon line(~%) to a normal line(~$)?
Thank you in advance
You can set your PS1 variable in your .bashrc, .bash_profile, or .profile to whatever you want. For example:
export PS1=“\h \W $ “
Will give your hostname, current working directory, followed by the $ you wanted. This can be customized however you like, see this link for a good description.
Also, as chepner mentioned in the comments, you need to set your default shell back to bash. I believe the easiest thing to do is go into your terminal settings, and change your shell:
Terminal > Preferences... > General > Shells open with: Command (complete path): /bin/bash
For a long time, the bash was a default shell in macOS. However, Apple replaced Bourne Again SHell with Z shell for licensing reasons
Set default shell to bash on Macos Catalina. The procedure is as follows:
Open the terminal application.
List available shells by typing cat /etc/shells.
To update your account to use bash run chsh -s /bin/bash.
Close terminal app.
Open the terminal app again and verify that bash is your default shell.

Issue with environment variable on Mac OS sierra

I am seeing a strange problem with the storing of an env in mac os.
I set custom env in ~/.bash_profile
export MYENV=user
Then ran the . ~/.bash_profile and then I printed the env using
printenv then I can see the MYENV=user in the list.
If I close the terminal and reopen and execute printenv then I could not see MYENV in the list still I can see the export MYENV=user in ~/.bash_profile. It seems strange to me.
I am using Mac os High Sierra 10.13.6.
Could some body please tell me what mistake I am doing?
Note that ~/.bash_profile is only run for login shells. 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 exe-
cutes 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.
So if you terminal isn't launching the shell with -l, --login or with $0 having a leading hyphen it won't be a login shell and thus won't read ~/.bash_profile. You may need to reconfigure how your terminal launches the shell if you want the shell to read that config script.
On the other hand ~/.bashrc is always read by an interactive shell. So if you put the export in that script it should do what you expect. It certainly does for me. You replied to Amila that it didn't work for you. So I'd suggest a simple experiment. Open two terminal windows. In one edit ~/.bashrc and add these two lines:
echo running .bashrc
export WTF=abc
In the other window just run bash. It should echo that message and echo $WTF should print abc. Now open a new terminal window. If you don't see that message and the env var isn't present then something is inhibiting reading that config script. Possibly the shell is being run with the --norc flag.
~/.bash_profile is executed before the initial command prompt is returned to the user, which means after a new login. Try adding the environment variable to ~/.bashrc instead.

"source" command on mac

I have a .bashrc file on Mac OS in which I set some aliases. After I make it and I type source .bashrc, the aliases work perfectly. However if open another shell, my shortcut command will not be recognized and I need to do source .bashrc again. How can it make it once and for all?
Terminal and iTerm 2 open new shells as login shells by default. When Bash is opened as a login shell, it reads ~/.bash_profile but not ~/.bashrc.
See https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html:
Invoked as an interactive login shell, or with --login
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.
So either:
Use ~/.bash_profile instead of ~/.bashrc.
Use ~/.bashrc but source ~/.bashrc from ~/.bash_profile.
Tell your terminal application to open new shells as non-login shells.
I have done the last two. For example tmux and the shell mode in Emacs open new shells as non-login shells. I still source ~/.bashrc from ~/.bash_profile because Bash is opened as a login shell when I ssh to my computer.
If you are on Mac and you want to source ~/.bash_profile automatically on when a terminal is opened
Open Terminal
Go to Preferences
Go to Profiles
Select then open shell
Add this command source ~/.bash_profile in Run command
If you want to source your bash_profile file everytime you open a new tab you can also set up a command on Iterm.
Go to Preferences -> Profiles -> General -> Command
I encounter the same problem and I solve it.
The macos have shift the default shell from bash to zsh.
So I try to modify the ~/.bashrc and ~/.bash_profile and source that but just work to current Terminal.
The fact is you should modify the ~/.zshrc profile file.
Try it my friend!
nano ~.bash_profile (Opens the ~.bash_profile)
aliasname(){
ssh user#ipaddress
}(You can add any command for the alias, I have shown the ssh command for a particular IP address)
Press Control+O and press Enter (Save the file). Control+X(Exit nano editor)
source .bash_profile
Now you have a persistent command across all the terminals whenever you type aliasname.

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.

"Git Bash here" is not preserving bash history between sessions [duplicate]

This question already has answers here:
.bash_history does not update in Git for Windows (git bash)
(11 answers)
Closed 2 years ago.
I am running Git-1.8.0-preview20121022 on Windows 7 and the install was with "Git Bash Only" (least intrusive to Windows cmd).
When I open the Git Bash from the start menu shortcut, everything is fine with the history.
But when the Git Bash here context menu (either the git-cheetah shell extension one or the simpler registry one) is what launched a session, the commands from that session are not saved to the .bash_history.
How could figure out why this is happening? Or better yet, does someone know how to fix this?
You should be able to fix this by adding this line to your ~/.bash_profile
PROMPT_COMMAND='history -a'
As mentioned here: https://stackoverflow.com/a/60718848/6680510
Create the following files
~/.bash_profile
~/.bashrc
And put the following line in both of them
PROMPT_COMMAND='history -a'
To do this from the console (git bash) itself use the following
commands
echo "PROMPT_COMMAND='history -a'" >> ~/.bash_profile
echo "PROMPT_COMMAND='history -a'" >> ~/.bashrc
What history -a means
From history --help command
-a append history lines from this session to the history file
What is PROMPT_COMMAND ?
Bash provides an environment variable called PROMPT_COMMAND. The contents of this variable are executed as a regular Bash command just
before Bash displays a prompt.
Difference between .bash_profile AND .bashrc
.bash_profile is executed for login shells, while .bashrc is
executed for interactive non-login shells.
When you login (type username and password) via console, either
sitting at the machine, or remotely via ssh: .bash_profile is executed
to configure your shell before the initial command prompt.
But, if you’ve already logged into your machine and open a new
terminal window (xterm) then .bashrc is executed before the window
command prompt. .bashrc is also run when you start a new bash instance
by typing /bin/bash in a terminal.
On OS X, Terminal by default runs a login shell every time, so this is
a little different to most other systems, but you can configure that
in the preferences.
References
https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html
https://apple.stackexchange.com/questions/51036/what-is-the-difference-between-bash-profile-and-bashrc
Putting
PROMPT_COMMAND='history -a ~/.bash_history'
into the .bash_profile did it for me.

Resources