Question

I am trying to create a permanent alias for my terminal. I put the alias in my ~/.profile, ~/.bashrc, and ~/.bash_profile files, previously empty. When I start a new terminal, bash does not recognize the alias, but if I source any of them, it does. Why are these not getting run when I open a terminal? I am on OSX.

Was it helpful?

Solution

Two things need to happen here when using iTerm to get the loading of dotfiles to work.

First you should add the following to your .bash_profile

[[ -s ~/.bashrc ]] && source ~/.bashrc

Secondly you will need to ensure that in iTerm preferences your terminal is set to launch a login shell.

iTerm Preferences

Hope this helps!

OTHER TIPS

Using the default mac terminal, what worked for me was to add a command to run on start up to source my .bash_profile.

Preferences > Profile > Startup > Add command 'source ~/.bash_profile'

Mac terminal preferences window screenshot

Might be considered to be a bit hacky, but it does the trick.

As of High Sierra, both Terminal and iTerm want to load ~/.profile first. So I suggest you put one line in your .profile to make your Mac work like other Unixes:

source ~/.bash_profile

By editing this one file, you won't have to search through the menus of multiple apps to override Apple's bizarre behavior in each.

Adding source ~/.profile to my .bash_profile worked for me.

Why are your shell's initialization files not loading?

As with most things, It Depends ™

I recently experienced the same phenomenon and went through the following exercise to resolve it:

I use iTerm. iTerm runs a login shell by default. Verify in iTerm Preferences > General > Command > (*) Login Shell Therefore, I know that ~/.bash_profile will always be called.

Knowing that, I put the following in my ~/.bash_profile file:

for file in ~/.{bashrc,bash_exports,bash_aliases,bash_functions}; do
    [ -r "$file" ] && source "$file"
done
unset file

Notice that I use separate files for .bashrc, .bash_exports, etc. It keeps things separate and simple.

Note also that /etc/profile is loaded first, but since I have never used that system wide init file, I knew that that was not my problem. For details check out $ man bash

So, I started with my ~/.bash_profile file.

I found that when I installed Canopy Express that it's installer replaced the contents of my ~/.bash_profile file with the following content:

# Added by Canopy installer on 2017-04-19
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make the bash prompt show that Canopy is active, otherwise 1
alias activate_canopy="source '/Users/lex/dev/python/User/bin/activate'"
# VIRTUAL_ENV_DISABLE_PROMPT=1 source '/Users/lex/dev/python/User/bin/activate'

p.s. Canopy is an excellent, free python IDE, that I highly recommend.

Fortunately, I backup my ~/.bash* files so restoring that was easy and quickly fixed my issue.

My advice would be to understand the order of calls to your initialization files and start with the first one and work your way through them until you find the problem.

Oh, and you may want to verify which shell you are using (I use bash):

~ $ echo $SHELL
/usr/local/bin/bash

I am guessing you may use another shell, such as bash, tcsh, sh, zsh etc.

Put source .bash_profile into your appropriate 'bashrc' file will make the auto loading recovered, i.e.

.login for tcsh, .bash_profile for bash, .zshrc for zsh

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top