12

I am trying to edit bash_profile file to add path to my sdk through the following command on my mac machine. sudo touch ~/.bash_profile; open -e ~/.bash_profile

It opens the file in TextEdit but does not allow me to edit it. Though i have given super-user permission and password while running the above command. It is giving me following message when i try to edit it.

"You don’t own the file “.bash_profile” and don’t have permission to write to it. You can duplicate this document and edit the duplicate. Only the duplicate will include your changes." I am new to mac. So please elaborate your answers.

FarheenP
  • 349
  • 1
  • 2
  • 11
  • By the way, you should not create the file as the super-user, The `~/` path is your own home directory, and the `~/.bash_profile` file should be owned by you. – Some programmer dude Jan 21 '15 at 11:36
  • 1
    I am not trying to create this file. It already exists. I am just trying to edit it to set a PATH variable. – FarheenP Jan 21 '15 at 12:18
  • Then why the `touch` command? And it seems that your user is not the owner of the file. If you do e.g. `ls -l ~/.bash_profile` what user does it list? And `sudo open -e ~/.bash_profile` doesn't work either (it should!)? – Some programmer dude Jan 21 '15 at 12:27
  • ls -l ~/.bash_profile shows following : -rw-r--r-- 1 root staff 104 Jan 21 17:26 /Users/farheen/.bash_profile – FarheenP Jan 21 '15 at 12:37
  • And sudo open -e ~/.bash_profile gives the sme permission denied error – FarheenP Jan 21 '15 at 12:38
  • 1
    The OSX `open` command may change user or something. As for the file, it should be owned by you (`farheen`). Change it using `chown`: `sudo chown farheen ~/.bash_profile`. After that you should not need to use `sudo` to edit it (just use the `open` command!, don't do `sudo touch`!). – Some programmer dude Jan 21 '15 at 12:43
  • @JoachimPileborg It would be great if u can post above as an answer so that i can accept the answer ! – FarheenP Jan 21 '15 at 13:25
  • Updated my existing answer – Some programmer dude Jan 21 '15 at 13:28

3 Answers3

19

The problem is that the file isn't owned by your user, but by root. You need to change the owner of the file to your user, then you can open and edit the file without the use of sudo:

sudo chown farheen ~/.bash_profile

This will, as the super-user, change the owner of the file back to you.

Sergio
  • 3,317
  • 5
  • 32
  • 51
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    sudo open -e ~/.bash_profile; I did this still i get the same error message. All i want to do is edit this file. – FarheenP Jan 21 '15 at 11:05
7

For Mac User Catalina,

.bash_profile not open, they need grant permission to edit this file. You can achieve by below steps

1) Open the terminal and enter below command ex: sudo chown iMac ~/.bash_profile

sudo chown {your username} ~/.bash_profile

2) Open editor in the terminal, here i used nano

nano .bash_profile

3) Edit your text you want.

4) To save the changes you've made, press Ctrl + O

5) To exit nano, press Ctrl + X

6) And finally to load your changes, enter below command

source .bash_profile
Hitesh
  • 896
  • 1
  • 9
  • 22
0

OR

You can give permission and make it executable file using like below:

That error likely means that 'bash_profile' isn't executable. If it's in the current directory, you can make it executable with chmod +x bash_profile.

Pradip Sutariya
  • 257
  • 2
  • 16