Alias can't save forever on Mac? - macos

I want to create my own alias to make some command more simpler.I add
alias ll='ls -l' in ~/.bashrc,like this:
ANDROID_NAME=/Users/smy/Library/Android/sdk
PYTHONPATH=/Library/Python/2.7/site-packages:$PYTHONPATH
PATH=$ANDROID_NAME/platform-tools:$PYTHONPATH:$PATH
export ANDROID_HOME
export PYTHONPATH
export PATH
#alias
alias ll='ls -l'
when I first add this alias to this file,I execute source command,like this:
source ~/.bashrc
then in this command window,it can works,but when I create a new command window,it can't recognize the ll alias,that is when I execute ll,such error exists:
-bash: ll: command not found
when I type source ~/.bashrc,it will work.
So my question is:
why the alias can't be recognized whenever I type it,why I must execute source command to make it work when new command window opened,and how I can resolve this. I'm working on mac,Anyone can teach me about this,thanks!

You need to use ~/.bash_profile or ~/.profile for login shells instead of ~/.bashrc. From the documentation:
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.
and:
When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.
When opening a new terminal window/tab, the shell should be opened as a login shell.

Insert the alias you need into ~/.bash_profile and ~/.bash
Go to Terminal -> Preferences -> Generals -> Shell opens with -> Command (complete path) -> /bin/bash

Only one things worked for me, and I haven't seen it mentioned yet.
In addition to
making sure that my .bash_profile file included the relevant alias/aliases
ensuring in the Terminal > Preferences > General window that the Shell opens with option selected is /bin/bash
I needed to ensure that the bash profile is loaded using bash -l each time a new window is opened. Probably spent 30m hunting for this, so hope it helps folks who are updating to the latest versions of their MacOS (like yours truly today)!

Related

Bash alias not saving beyond one session?

I'm trying to make a bash alias for traversing through a few folders, but the alias does not save after I close terminal.
I've already saved the alias in the .bashsrc file and have also have run the command . ~/.bashsrc.
Here's what I've done:
sudo nano .bashsrc
Inside bashsrc:
alias x = 'cd Documents/Photos/Family'
And then saved and exit.
. ~/.bashsrc
The alias works in that terminal window, but shows "command not found" if I restart terminal or open a new tab.
You should prefer setting your changes in ~/.bashrc and ~/.bash_profile.
alias x='cd Documents/Photos/Family'
Also, remember aliases won’t be exported to subshells and while using this alias you always need to be present in the directory where Documents is present. I guess your Documents directory is present in home, so it would be better if you do something like:
alias x="cd "$HOME"/Documents/Photos/Family"
Just add this line to the end of both the files and save.
In simple language there are two types of shells for your case, login and non-login interactive shell.
Before starting of a login shell, .bash_profile is sourced and before starting of a non-login shell .bashrc is sourced.
So you should add your changes in both.
If you are on macOS, .bashrc is next to useless for your case. MacOS would treat every shell you open in terminal as a login shell. Although there are some exceptions which i don’t remember.
As mentioned by David in the comments, in some distros ~/.bash_profile is just ~/.profile (e.g. Debian, openSUSE, etc).

Issue with aliases in Terminal shell

I'm set /bin/bash as my default shell in Terminal (macos) and my aliases, written in '~/.bashrc' don't work (command not found).
But if I write in Terminal /bin/bash command, I'll switch to new bash3.2 shell, and then I can use it.
Is there some another '.bashrc' where I should write down my aliases?
P.S. I used Terminal preferences to made /bin/bash default
and chsh -s /bin/bash/ command too.
Terminal starts a login shell rather than an "ordinary" interactive shell, because Terminal itself is not run from a shell that inherited an environment from a login shell.
Ordinary interactive shells source .bashrc; login shells source the first of .profile, .bash_login, or .bash_profile it finds. I recommend sourcing .bashrc directly from .bash_profile.
Your aliases work when you run /bin/bash directly because that does not start another login shell, and so .bashrc is sourced as expected.
As above, if you add the following line to .profile in your home directory:
source ~/.bashrc
Once you save it and create a new Terminal session, it will automatically source your .bashrc with your alias settings.
You want /bin/bash -l to run as if you used a login shell. See the INVOCATION section of man bash for more information.

How to load ~/.bash_profile when entering bash from within zsh?

I've used bash for two years, and just tried to switch to zsh shell on my OS X via homebrew. And I set my default (login) shell to zsh, and I confirmed it's set properly by seeing that when I launch my Terminal, it's zsh shell that is used in default.
However, when I try to enter bash shell from within zsh, it looks like not loading ~/.bash_profile, since I cannot run my command using aliases, which is defined in my ~/.bash_profile like alias julia="~/juila/julia", etc.. Also, the prompt is not what I set in the file and instead return bash-3.2$.
For some reasons, when I set my login shell to bash, and enter zsh from within bash, then ~/.zshrc is loaded properly.
So why is it not loaded whenever I run bash from within zsh? My ~/.bash_profile is symbolic linked to ~/Dropbox/.bash_profile in order to sync it with my other computers. Maybe does it cause the issue?
Open ~/.zshrc, and at the very bottom of the file, add the following:
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile;
fi
Every time you open the terminal, it will load whatever is defined in ~/.bash_profile (if the file exist). With that, you can keep your custom settings for zsh (colors, and etc). And you get to keep your custom shell settings in .bash_profile file.
This is much cleaner than using bash -l IMO.
If you prefer putting your settings in .bashrc, or .bash_login, or .profile , you can do the same for them.
Similarly, you could also move the common profile settings to separate file, i.e. .my_common_profile, and add the following to both .bash_profile and .zshrc:
if [ -f ~/.my_common_profile ]; then
. ~/.my_common_profile;
fi
An interactive bash reads your ~/.bash_profile if it's a login shell, or your ~/.bashrc if it's not a login shell.
A typical .bash_profile will contain something like:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
so .bashrc can contain commands to be executed by either login or non-login shells.
If you run bash -l rather than just bash, it should read your .bash_profile.
Reference: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
For those who have just installed zsh and want their alias from bash to work on zsh do the following
Open .zshrc file in vim like so
vi ~/.zshrc
Scroll to the bottom
click "i" to enable write mode
Tell zsh to load items from bash_profile when needed like so
source ~/.bash_profile
Write and quit like so
:wq
Refresh your zsh like so
source ~/.zshrc
That's it. Now all your saved alias in .bash_profile will be ready to use in zsh.
To complement #Keith Thompson's excellent answer:
macOS:
As #chepner puts it succinctly (emphasis mine):
In OS X, bash is not used as part of the initial [at boot time] login process, and the Terminal.app (or other terminal emulators) process exists outside any pre-existing bash sessions, so each new window [or tab - read: interactive bash shell] (by default) treats itself as a new login session.
As a result, some OSX users only ever create ~/.bash_profile, and never bother with ~/.bashrc, because ALL interactive bash shells are login shells.
Linux:
On Linux, the situation is typically reversed:
bash shells created interactively are [interactive] NON-login shells, so it is ~/.bashrc that matters.
As a result, many Linux users only ever deal with ~/.bashrc.
To maintain bash profiles that work on BOTH platforms, use the technique #Keith Thompson mentions:
Put your definitions (aliases, functions, ...) in ~/.bashrc
Add the following line to ~/.bash_profile
[[ -f ~/.bashrc ]] && . ~/.bashrc
Copy the contents from ~/.bash_profile and paste them at the bottom of ~/.zshrc file.
For ZSH users on MacOs, I ended up with a one liner.
At the very bottom of the ~/.zshrc I added the following line :
bash -l
What it does is simply load the .bash_profile settings(aliases, function, export $PATH, ...)
If you decide to get rid of ZSH and go back to plain BASH, you'll be back to normal with no hassle at all.
If this is something that you do infrequently, or it just isn't appropriate to make changes, you can also 'source' the .bash_profile after launching the child bash shell.
. ~/.bash_profile
This will pull in the settings you make in the .bash_profile script for the life of that shell session. In most cases, you should be able to repeat that command, so it's also an easy way to test any changes that you make without needing to do a full login, as well as bring all of your existing shell sessions up-to-date if you make upgrades to the .bash_profile &/or .bashrc files.
For macOS Big Sur (Version 11.5.2)
Open .zshrc
For example: sudo nano ~/.zshrc
At the end of the file add source ~/.bash_profile
Every time you open the terminal the contents inside the bash profile will be loaded.
Recently I installed oh-my-zsh on OS X and set zsh as default shell and faced the same problem.
I solved this problem by adding source ~/.bash_profile at the end of .zshrc file.
I am using a zsh framework called oh my zsh and I have tried most of the solutions listed here and it broke the format for my custom theme. However, these steps worked for me.
Add new alias(es) at the bottom of my .bash_profile
vi ~/.bash_profile
Make zsh to load items from .bash_profile
source ~/.bash_profile
Refresh zsh
source ~/.zshrc
Restart OSX Terminal app
Try your new alias!
If you'd like to be "profile-centric", you can create .profile as a single source of truth, then load it from both .bash_profile and .zprofile.
.profile
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
# etc., etc.
.bash_profile and .zprofile
if [ -f ~/.profile ]; then
. ~/.profile;
fi
I found this helped bash scripts find the right PATH, etc., and helped me keep configuration in one place.

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

Resources