How do I add the bin subdirectory of the first directory in GOPATH to PATH?

You can use:

PATH="$PATH:${GOPATH%%:*}/bin"

Or

PATH="$PATH:${GOPATH%:*}/bin"

Both will work because there can be at most one :.

It will remove the part after :. So, in your first case, it will remove the second directory and in your second case, there will be no pattern like :*, so there will be no change in the directory name.


PATH="$PATH:${GOPATH%%:*}/bin"

The asterisk is a glob, not a regular expression. Two percentage signs means to remove the maximum that it can match from the back, so even if there were three or more directories, you would only get the first one.

If there's nothing to remove, it doesn't remove anything, so you get the only path if there's only one.

Tags:

Linux

Shell

Bash