'"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?
Solution 1:[1]
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.
Solution 2:[2]
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
Solution 3:[3]
If you are on Mac and you want to source ~/.bash_profile
automatically on when a terminal
is opened
Solution 4:[4]
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!
Solution 5:[5]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | alakin_11 |
Solution 3 | Umer Waqas CEO Fluttydev |
Solution 4 | Pandy |
Solution 5 | Mohit Hapani |