49

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

  • Can you post the relevant parts of your `.bash_profile`? Do you have a `.bashrc`? – Philippe Signoret Mar 31 '13 at 19:32
  • @PhilippeSignoret : Thanks, yes I have both. Posted them in here: https://gist.github.com/babakinks/5281748 –  Mar 31 '13 at 19:42
  • 1
    If you add an `echo "Hello, World!"` towards the end of `.bash_profile`, do you see that it gets executed when you open a terminal? – Philippe Signoret Mar 31 '13 at 19:54
  • Are you even using BASH? Try "chsh" if not. – Ulrich Eckhardt Mar 31 '13 at 20:02
  • @PhilippeSignoret : I added your echo command to the end of that bash_profile, quit my iTerm2 terminal..opened it again..so I should see a hello world message? I did NOT see that. – DarkNightFan Mar 31 '13 at 20:05
  • @PhilippeSignoret : I also have a .zshrc for z-shell... I added a echo to that one... and it shows the message from that one. –  Mar 31 '13 at 20:09
  • 2
    ... which obviously means you should add all the lines from 6 to 13 to your `.zshrc`. – TC1 Mar 31 '13 at 20:24

7 Answers7

99

Now that we've narrowed down the problem:

  1. Run ps -p $$ at the command line to check if you are, in fact, using a bash shell.
  2. Realize that you are in zsh, which means you should be editing your profile in .zshrc.
  3. Copy the offending lines from .bash_profile to .zshrc, OR
  4. 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.

Philippe Signoret
  • 13,299
  • 1
  • 40
  • 58
  • 2
    Well a blatantly obvious reason **not** to source a **Bash specific** configuration file from Z shell would be the possibility for it to contain **Bash specific** configuration... Copying the lines over or externalizing them to some `PATH` setup file & sourcing from both would be appropriate. – TC1 Mar 31 '13 at 21:05
  • 1
    @TC1 Yes, that is the type of reason I was referring to. :) So keep it clean and do what TC1 says. – Philippe Signoret Mar 31 '13 at 21:13
  • Thank you this is what i was looking for – Rishabh Agarwal Apr 03 '20 at 20:33
  • 1
    This is the best answer I've seen in all my years as an engineer. Correct, made me laugh and still was polite. Fixed my problem and made my day. Thank you. – Ethan_AI Sep 14 '22 at 19:24
25

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.

Brent
  • 1,894
  • 1
  • 15
  • 17
  • LOL I was using zsh this whole time because I guess my company laptop came with it set that way. Thanks! – Samuel Dominguez Jul 22 '20 at 23:10
  • Since this was what worked for me i would like a little more verbose if possible, since i couldn't understand half of it. Just went ahead with the creation of .zshrc and copy the stuff. How did you opened preferences from terminal? How did you change the opening option? – Mbotet Dec 29 '22 at 10:35
  • 1
    @Mbotet, I updated it with more instructions. Thanks. – Brent Jan 20 '23 at 15:52
21

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

enter image description here

echo $SHELL will display the currently active shell in your machine

enter image description here

To change active shell to a different shell, use chsh -s /bin/bash. Then echo $SHELL to verify if the shell has changed.

divine
  • 4,746
  • 3
  • 27
  • 38
12

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

Nawaf Chowdhury
  • 121
  • 2
  • 2
1

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.

Sean Y
  • 11
  • 3
0

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.

Kevin Danikowski
  • 4,620
  • 6
  • 41
  • 75
0

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69