3

I want to run executables in my $GOPATH/bin directory without having to cd there so I've added the following to my .profile file:

    export GOBIN=$HOME/go-workspace/bin
    function _rungo() { "$GOBIN" "@$1" }
    alias rungo='_rungo'

The alias definition is on line 28. The idea is that on the command line I type rungo executable_name to run an executable.

When I log into my account, I get the following error:

     line 31: syntax error: unexpected end of file

If I comment out the alias definition I get this error

    line 29: syntax error: unexpected end of file

So obviously my function definition is incorrect.

What am I doing wrong?

Steve Murphy
  • 431
  • 2
  • 7
  • 17
  • Why define an alias instead of just naming the function `rungo` to start? – chepner Oct 26 '16 at 11:34
  • If just naming the function is sufficient that would be fine. I haven't done Linux for a while so I don't know what the best solution is. – Steve Murphy Oct 26 '16 at 19:31
  • Use `shopt -sq expand_aliases` before `alias...` (Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt, man 1 bash). – Alexander Lubyagin Dec 07 '18 at 20:52

2 Answers2

2

If you declare a bash function as a one liner, you need to include a ; before you end the function, thus the working function should be

function _rungo() { "$GOBIN" "@$1" ;}

EDIT:

Answer to the following:

Thanks Karoly and Zuzzuc. I've decided to keep the function as a one-liner so I added the ; When I issue a command like "rungo hello" I get a message that says /home/smurphy/go-workspace/bin is a directory Is it possible to do what I want to do?

Well, first of all you do not need to write function myFunction() because myFunction() works just as well.

Moving on to the real question, you does not need to make the alias rungo, just name the function rungo instead. Creating the alias is just a waste of time, as it will redirect its input to the function you created.

Partly the reason it failed, is because aliases can not take arguments. I am also not sure wether you need the "@" in "@$1".

I does also think it would be better to use exec "$GOBIN/$1" instead of just "$GOBIN" "@$1".

Solution

Change

export GOBIN=$HOME/go-workspace/bin
function _rungo() { "$GOBIN" "@$1" }
alias rungo='_rungo'

to

export GOBIN=$HOME/go-workspace/bin
function rungo() { exec "$GOBIN/$1" ;}

Hoped this helped!

Zuzzuc
  • 148
  • 9
  • Thanks Karoly and Zuzzuc. I've decided to keep the function as a one-liner so I added the `;` When I issue a command like "rungo hello" I get a message that says _/home/smurphy/go-workspace/bin is a directory_ Is it possible to do what I want to do? – Steve Murphy Oct 26 '16 at 19:48
  • I'll edit my answer instead because it supports better formatting. – Zuzzuc Oct 26 '16 at 20:33
  • Thanks, Zuzzuc. Works perfectly. – Steve Murphy Oct 31 '16 at 15:05
0

The function closing bracket has to be in a new line. Alternatively, use ;.

To illustrate the problem, look at this example:

function rungo() { echo X }
}
rungo    # outputs: X }
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176