Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Alias for Function in .profile

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?

like image 852
Steve Murphy Avatar asked Feb 12 '26 03:02

Steve Murphy


1 Answers

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!

like image 196
Zuzzuc Avatar answered Feb 14 '26 21:02

Zuzzuc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!