Bash .profile not loading - macos

I'm not sure what's happened but my ~/.profile is no longer loading.
Can anyone see something wrong with the following?
export PS1="\u#local [\w]# "
export EDITOR="subl -w"
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
alias vst="ssh -i ~/.ssh/vst root#vst"
I know for a fact using that PS1 like I am attempting to do it should be doing Peter#local [~/path/to/file]# but it's not.
Any ideas?

Does ~/.bash_profile or ~/.bash_login exist? If so, that'll be used instead of ~/.profile.

In Unix FAQ (for OS X) we can read:
Bash Startup Files
When a "login shell" starts up, it reads the file
/etc/profile and then ~/.bash_profile or ~/.bash_login or
~/.profile (whichever one exists - it only reads ONE of these,
checking for them in the order mentioned).
When a "non-login shell" starts up, it reads the file /etc/bashrc and then the file ~/.bashrc.
Note that when bash is invoked with the name sh, it tries to mimic the startup sequence of the Bourne shell (sh). In particular, a non-login shell invoked as sh does not read any dot files by default. See the bash man page for details.
So if you already have ~/.bash_profile, the file ~/.profile won't be automatically read by bash, therefore you can add the following lines in your ~/.bash_profile to load it:
# Load user profile file
if [ -f ~/.profile ]; then
. ~/.profile
fi

Related

Appending bash alias to .bashrc doesn't work

I want to create a alias of my cd command. I have created the .bashrc file and append the command cd ...... to it. (Since the file was newly created, it just has this one line that I added).
After that, only after I typed . ~/.bashrc, can the alias works. If I close the terminal and open it again, I need to retype . ~/.bashrc.
It's really annoying to do this every time. Is there any way to solve this problem?
Thank you so much for help
When you login to linux system, only ~/.profile will be called:
$ cat ~/.profile
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
You need to source ~/.bashrc inside ~/.profile manaully.
Read this to learn more.
EDIT:
If you're using iTerm2 on mac, it actually start a login shell by default when open tabs.
But you can change it: Preferences > General > Command
If using OS X, append the alias to ~/.bash_profile.
You could also add alias to ~/.bashrc, then add source ~/.bashrc to ~/.bash_profile.
Better yet, put all your aliases in ~/.aliases, and source it in ~/.bash_profile.
By default, OS X first sources /etc/bashrc (which shouldn't be modified unless absolutely necessary), then sources the user's ~/.bash_profile at the start of every interactive session.

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.

How to make OS X to read .bash_profile not .profile file

I have read so many suggestions about, not putting your customization aka commands in ".profile" file. Rather, create a .bash_profile for yourself and add your alias and etc.
But,when I open the new terminal, if there is only .bash_profile, OS X is not exporting/sourcing the commands mentioned in it. I have to manually source the .bash_profile.
If I create .profile file, on opening a new terminal, all my commands in .profile are executed and will be available readily.
Could you please help me in understanding, how does it works? Also, when to use .bashrc/.profile/.bash_profile files.
Thanks!
According to Apple,
zsh (Z shell) is the default shell for all newly created user accounts, starting with macOS Catalina.
So you should verify your default shell with the command:
$ echo $SHELL
If the result is /bin/bash your default shell is BASH, and if the result is /bin/zsh the default is ZSH.
Go to home with $ cd ~/ and create the profile (if it does not exist) and edit it with the commands:
For bash:
$ touch .bash_profile
$ open .bash_profile
For ZSH:
$ touch .zprofile
$ open .zprofile
According to the manual page that ships with OS X:
... 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.
It should only read ~/.profile as a last resort if neither ~/.bash_profile nor ~/.bash_login are readable.
On all of my OS X systems, I have my ~/.bash_profile set to:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
It is highly recommended that you do this on OS X in order to get bash to read your ~/.bashrc file like you would expect.
It's also possible that your terminal shell is defaulting to sh instead of bash. You can verify this first:
$ echo $SHELL
/bin/tcsh
To change this to bash, you can go into your Terminal -> Preferences -> Startup tab, and change "Shell Opens With:" from "Default login shell" to Command and value "/bin/bash".
Alternately, you can change your default shell by executing the following command at the command prompt:
chsh -s bin/bash
After you do one of these, open a new shell window, and your .bash_profile should be sourced.
For anyone else who finds this, instead of bash_profile, for new versions of mac you can use .zshrc. I.E., do
open .zshrc
and add what you need there.
You can use zsh to fix the problem.
The Z shell (also known as zsh) is a Unix shell that is built on top
of bash (the default shell for macOS) with additional features. It's
recommended to use zsh over bash.
Installation
Install zsh using Homebrew: $ brew install zsh
Install Oh My Zsh: $ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Move to .bash_profile setting .zshrc file
To apply the changes you make you need to either start new shell
instance or run: source ~/.zshrc
If you are using zsh, you can source to .bash_profile by adding the following line to .zprofile
if [ -f ~/.bash_profile ]; then
source ~/.bash_profile
fi
It should be mentioned that bash will first look for a /etc/profile file, as stated in the Bash man pages.
When bash is invoked as an interactive login shell, or as a non-inter-
active shell with the --login option, it first reads and executes com-
mands 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.
I solved by simply adding bash (in a newline) into ~/.bash_profile file.

Why do I have to keep using `source ~/.profile` to get settings in place?

I have a couple of bash scripts that I want to make sure runs by default and I'm currently storing them in ~/.profile on my mac. Is that the wrong place to be storing them? I've heard of others and tried them (like ~/.bashrc, ~/.bash_profile, etc), but they don't seem to be working.
What is the difference between all of these and which one do I put the scripts in so that it configures on runtime and I don't have to call $ source ~/.profile every time I open the terminal?
If both ~/.bash_profile and ~/.profile exist, bash only reads ~/.bash_profile when it is invoked as an interactive login shell.
https://www.gnu.org/s/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.
[...]
Invoked as an interactive non-login shell
When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.
~/.profile is also used by other shells.
Terminal and iTerm open new shells as login shells by default (by executing something like login -pf $USER), but many GNU/Linux terminal applications open new shells as non-login shells. OS X users often use ~/.bash_profile instead of ~/.bashrc.
+-----------------+
| |
interactive shell -->| ~/.bashrc |
| |
+-----------------+
interactive shell will source ~/.bashrc automatically.
Take a look at Should the .bashrc in the home directory load automatically?
I did these to rectify the problem:
cat .bash_profile >> .profile
rm .bash_profile
the alternative is:
echo "source ~/.profile" >> .bash_profile
Make sure if you do source ~/.profile in your .bashrc that you comment out or remove any commands (in .profile) to call or source .bashrc in your .profile or it will loop forever and you will never get a prompt.
Different setups of bash will automatically source different files depending on their configuration. The nearly universal file that is always sourced is ~/.bashrc - this is a bash core thing that it will load this file. In that file, you should add your line to source ~/.profile and you'll be good to go!
-Edit-
From my Linux and my colleague's Mac:
$ echo "echo hello" >> ~/.profile
$ echo "source ~/.profile" >> ~/.bashrc
$ bash
Hello
$

Hiding the text before an input to only $-sign in Bash?

I have this in .bashrc;
PS1='$'
However, I see this still in terminal:
mas-macbook:some/path mas$
I want
$
PS1 should already have been exported long before you get to your .bashrc file, at least for a login shell. In that case, setting PS1 should simply overwrite the value (not its export status).
One thing to keep in mind is that bash itself does not run your .bashrc file for a login shell. The actual sequence of execution is:
/etc/profile, if there.
first of ~/.bash_profile, ~/.bash_login or ~/.profile.
I'm fairly certain that, if you want .bashrc to run for a login shell, it has to be sourced from one of those above.
For example, /etc/profile may call /etc/profile.local or all the scripts in the /etc/profile.d/ directory. Similarly, my .bash_profile calls the following, if they exist:
/etc/bash.bashrc
~/.bashrc
with the following snippet:
if [ -e /etc/bash.bashrc ] ; then
source /etc/bash.bashrc
fi
if [ -e "${HOME}/.bashrc" ] ; then
source "${HOME}/.bashrc"
fi
When I change PS1 and echo "hello" in my .bashrc, but comment out the sourcing of it in .bash_profile, the prompt doesn't get changed (nor the string printed) when I log in. When I uncomment the sourcing, I get both the string printed and the prompt changed when I log in.
To make sure that your .bashrc is called for your login shells, put that echo hello statement just after setting PS1, then log in to check.
If it is being called when you log in, you can execute "export -p" from your shell to get a list of all the exported variables - make sure PS1 has a "declare -x" in front of it. If not, just change your .bashrc to export it as well:
export PS1='$'
If it's already exported, then something is changing it after your set statement. In that case, you'll need to actually look at the login execution path to see what's getting called before it gives you control.
I think you need the export keyword:
export PS1='$'
You may have to escape the $ symbol. This works in my system...
PS1="\$"
If that don't work, please run the command 'echo $PS1' and let us know the results. You may also have to send the .bashrc file - it possible that you have set the PS1 variable before including the global /etc/bashrc file. If that's the case, the global file will overwrite the variable. You will have to set the PS1 variable after including the /etc/bashrc file.

Resources