-1

When I save this to my ~/.bashrc file I get an error upon running source ~/.bashrc ? Anyone know what I'm doing wrong here?

# Git branch in prompt.

parse_git_branch () {
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}

ERROR

$ source  ~/.bashrc

: command not found
'bash: /Users/RGA/.bashrc: line 3: syntax error near unexpected token `{
'bash: /Users/RGA/.bashrc: line 3: `parse_git_branch () {

NOTE: the only thing in my ~/.bash_profile is:
source ~/.bashrc

UPDATE TO INCLUDE BASH VERSION (OSX 10.9)

$ bash --version 

GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin13)  
Copyright (C) 2007 Free Software Foundation, Inc.
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
hackg
  • 95
  • 1
  • 6
  • I don't see anything wrong with it, and in fact it works fine for me, albeit with a newer `bash`. You could try removing the space between the function name and parentheses. – John Bollinger Apr 17 '16 at 05:48
  • 1
    DOS line endings are an incredibly common source of errors. Voting to close as typo. – tripleee Apr 17 '16 at 07:53

1 Answers1

2

There is something suspicious about the error output. Let's look at the first two errors, and think about the fact that your file has a comment as its first line:

: command not found

What command might this be? There is no command name here. Hmm...

'bash: /Users/RGA/.bashrc: line 3: syntax error near unexpected token `{

And: why is there a closing single quote at the beginning of the line, and then an opening single (back)quote near the end of the line, just before the open brace {?

What if each of these first three lines ends with a mostly-invisible character that, when printed, tells the computer to move back to the first position of the current line? Let's represent this hypothetical mostly-invisible character as \r, which stands for "invisible character that causes a position-return-to-beginning-of-line". Then the command that is not found is this invisible-character \r command, and the syntax error occurs because the open brace { is followed by \r.

Perhaps we should write this character instead as ^M, where the M stands for "motion to beginning of line". Perhaps your first three lines are not:

# Git branch in prompt.

parse_git_branch () {

but rather:

# Git branch in prompt.^M
^M
parse_git_branch () {^M

Perhaps you should view the file with an editor that does not hide carriage-returns \r (aka ^M) that occur before newlines \n.

(If your editor is vim you can use this setting, which some will probably call overkill, but which I like:

set ffs=unix " disable "file format = dos" detection

Or you can set the file format back once the file is open, so that vim won't re-add the ^M at the end of each line. If you use some other editor, find out how to make it stop doing automatic DOS-style line endings.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • That's not what the `M` in `^M` stands for (although it does make for a nice mnemonic). Control characters are represented by the character which, if you clear the 6th bit, gives you the control character. M is ASCII 77; carriage return is ASCII 13 = 77 & 0xbf. – chepner Apr 17 '16 at 13:10
  • @chepner: I know, I was just playing with it. Besides the control series there's also the old fashioned meta series (these days, replaced with ISO Latin 1 or other code pages, but still in vis(3) in the form I suggested back in the 1990s: `\^C` means control, `\M-C` means meta, and `\M^C` means meta-control). – torek Apr 17 '16 at 19:39
  • Thanks @torek - it was indeed a hidden character that was causing all my fuss. I recreated my bashrc file from a blank txt file and typed the necessary code back... everything worked fine. I guess something carried over when I copied it from somewhere or another file – hackg Apr 17 '16 at 23:59