47

So I'm installing some things for coding and personal usage, and I need to run this in the terminal (I'm on Mac if you didn't read the title).

~/.bash_profile

It just says permission denied, Im running OSX 10.8.4 Mountain Lion. How do I bypass this?

crmpicco
  • 16,605
  • 26
  • 134
  • 210
Nick Wetter
  • 471
  • 1
  • 4
  • 3

7 Answers7

68

On MacOS: add source ~/.bash_profile to the end of ~/.zshrc. Then this profile will be in effect when you open zsh.

Anatoly
  • 20,799
  • 3
  • 28
  • 42
zhao
  • 1,094
  • 9
  • 9
43

You would never want to run that, but you may want to source it.

. ~/.bash_profile
source ~/.bash_profile

both should work. But this is an odd request, because that file should be sourced automatically when you start bash, unless you're explicitly starting it non-interactively. From the man page:

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.

kojiro
  • 74,557
  • 19
  • 143
  • 201
  • 1
    Good answer. But there is one time you might want to source: if you've just created it, and you want it to apply to the current session. Which sounds like exactly the OP's case. (That being said, anyone who doesn't already know the difference between `~/.bash_profile` and `. ~/.bash_profile` is a lot better off just creating a new session—e.g., New Tab in Terminal.app, then close the old one.) – abarnert Sep 16 '13 at 05:16
  • This helped me. But I had to manually source it, even restarting the terminal didn't help. – Saurabh Patil Aug 11 '19 at 05:37
10

If you change .bash_profile, it only applies to new Terminal sessions.

To apply it to an existing session, run source ~/.bash_profile. You can run any Bash script this way - think of executing source as the same as typing commands in the Terminal window (from the specified script).

More info: How to reload .bash_profile from the command line?

Bonus: You can make environment variables available to OSX applications - not just the current Bash session but apps like Visual Studio Code or IntelliJ - using launchctl setenv GOPATH "${GOPATH:-}"

Peter Tseng
  • 13,613
  • 4
  • 67
  • 57
  • 2
    it worked for me thanks , i just entered the cmd source .bash_profile from my home directory where i created this .bash_profile – Bravo Jan 12 '17 at 07:37
  • 1
    Missing the path (assuming not sitting in the user's root folder): source ~/.bash_profile – mobibob Oct 02 '19 at 15:48
1

As @kojiro said, you don't want to "run" this file. Source it as he says. It should get "sourced" at startup. Sourcing just means running every line in the file, including the one you want to get run. If you want to make sure a folder is in a certain path environment variable (as it seems you want from one of your comments on another solution), execute

$ echo $PATH

At the command line. If you want to check that your ~/.bash_profile is being sourced, either at startup as it should be, or when you source it manually, enter the following line into your ~/.bash_profile file:

$ echo "Hello I'm running stuff in the ~/.bash_profile!"
Matthew Turner
  • 3,564
  • 2
  • 20
  • 21
1

No need to start, it would automatically executed while you startup your mac terminal / bash. Whenever you do a change, you may need to restart the terminal.

~ is the default path for .bash_profile

Easwaramoorthy Kanagaraj
  • 3,925
  • 8
  • 36
  • 62
1

I was getting this error on zsh(mac os Big Sur 11.3), This is how i solved this :-

Go to Terminal.

cd /users/<yourusername>

Once you reach here issue a command :

ls -al

You will see a lot of files and one specific file .zprofile. This is your user profile. We need to edit this.


profile file image

After this we need to edit the file. Issue the below command :

nano .zprofile

Once you issue this command file will be opened for edit. Add the path details for maven.

M2_PATH="/Users//code/apache-maven-3.8.1/bin" //add your path of maven diretory PATH="${PATH}:${M2_PATH}" export PATH

press ctrl + X and save the file.

enter image description here

Issue command after saving the file :

source .zprofile

Once done, you will be able to run the mvn command.

Msharma
  • 31
  • 3
0

If the problem is that you are not seeing your changes to the file take effect, just open a new terminal window, and it will be "sourced". You will be able to use the proper PATH etc with each subsequent terminal window.

beroe
  • 11,784
  • 5
  • 34
  • 79