0

I've made a bunch of commands to use as shortcuts and they work but I wanted to make a command that would shortcut and open the in-terminal text editor, nano, with the .bash_profile.

Essentially instead of typing

sudo nano /Users/myUserName/.bash_profile

(the sudo is so I can save edits)

I want a command that I put in the .bash_profile to do that for me, manually adding this line of code to it:

alias commands='sudo nano /Users/myUserName/.bash_profile'

The problem is, when I run the command it just opens the file in textedit and it's a locked file meaning I can't edit.

Is there a way to do this?

l'L'l
  • 44,951
  • 10
  • 95
  • 146
Michael Zakariaie
  • 165
  • 1
  • 1
  • 10

2 Answers2

3

fix permissions on .bash_profile

.bash_profile should normally be owned by the user, not by root.

I'd suggest reverting it back to the right user using sudo chown myUserName /Users/myUserName/.bash_profile and then stop using that sudo command completely. There should be no need to have superuser power to edit that file to start with.

IMHO you're making it more difficult than it needs to be.

do not edit your files with root permissions.

Once you restore the normal ownership, groups and permissions, all you need is to type

nano ~/.bash_profile or whatever your favorite editor might be.

sudo in front of a command uses superuser (root) as the effective userid, use it sparingly and intentionally. It's a bit like the dark side of the force: yes the superuser can do more, but once you start using it, going back to the light side is difficult as all your stuff will be owned by the superuser and you'll start seeing things fail that should not fail.

Apply an edited .bash_profile in an already running shell

A copy of bash already running will not automagically pick up any change you make to your .bash_profile . It needs to be told to pick up the changes.

Either you start a new shell by starting another terminal.

Or you can also apply some changes (but maybe not undo all what was in the original!) .bash_profile by "sourcing" the file:

. ~/.bash_profile will do that for you - but there are limits to what it can do (e.g. if you defined an alias for ls, removing that line and sourcing the bash_profile again will NOT remove the alias form your running shell, you'll have to do it yourself, or quit that shell.

See also:

Community
  • 1
  • 1
  • You should properly suggest to change the group as well. – Andreas Louv Jun 04 '16 at 00:05
  • @andlrc : might be an issue, but it's less likely to affect his ability to write to the file. I'm actually hoping the OP posts the output of `ls -l` of the file as it is currently ... hard to predict what happened to it in the past. –  Jun 04 '16 at 00:08
1

I would suggest first changing the (correct) permissions/ownership of ~/.bash_profile. Then you could make a function for a command to edit it; also try setting nano as your default editor:

$ sudo chown user:staff ~/.bash_profile

This should change the permissions (substitute user and group for yours). Then if you want a shortcut to edit the file add this to ~/.bash_profile:

# Preferred editor (nano)
export EDITOR=nano

# Edit ~/.bash_profile in nano
edbp () { nano ~/.bash_profile ; }

Once added and saved relaunch Terminal. The command below should nano ~/.bash_profile:

$ edbp

To quickly/easily edit your profile.

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • Hard to tell what else is in that .bash_profile and/or if the OP knows to how to source it in the running shell etc. Creation of an alias to edit .bash_profile doesn't sound promising to what one might find inside ... –  Jun 04 '16 at 00:22
  • I deleted my comments, I see you have improved your answer ! – Andreas Louv Jun 04 '16 at 08:33