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

Clash Royale CLAN TAG#URR8PPP
echo $GOPATH will print:
/mnt/star/program/go/package:/mnt/star/git_repository/workspace/go_workplace
There are 2 directories, I want to append the first directory's sub-directory bin/ to $PATH.
If I write $PATH=$PATH:$GOPATH/bin, then actually it appends 2 directories to $PATH:
/mnt/star/program/go/package
This only contains directories, it should be/mnt/star/program/go/package/bin./mnt/star/git_repository/workspace/go_workplace/bin
This actually shouldn't be added to$PATH.
BTW, there are cases that $GOPATH only contains one directory, so that simply appending $GOPATH/bin will work.
I am looking for a solution that fits both cases. So, how do I write this in a bash config file?
linux bash shell
add a comment |
echo $GOPATH will print:
/mnt/star/program/go/package:/mnt/star/git_repository/workspace/go_workplace
There are 2 directories, I want to append the first directory's sub-directory bin/ to $PATH.
If I write $PATH=$PATH:$GOPATH/bin, then actually it appends 2 directories to $PATH:
/mnt/star/program/go/package
This only contains directories, it should be/mnt/star/program/go/package/bin./mnt/star/git_repository/workspace/go_workplace/bin
This actually shouldn't be added to$PATH.
BTW, there are cases that $GOPATH only contains one directory, so that simply appending $GOPATH/bin will work.
I am looking for a solution that fits both cases. So, how do I write this in a bash config file?
linux bash shell
add a comment |
echo $GOPATH will print:
/mnt/star/program/go/package:/mnt/star/git_repository/workspace/go_workplace
There are 2 directories, I want to append the first directory's sub-directory bin/ to $PATH.
If I write $PATH=$PATH:$GOPATH/bin, then actually it appends 2 directories to $PATH:
/mnt/star/program/go/package
This only contains directories, it should be/mnt/star/program/go/package/bin./mnt/star/git_repository/workspace/go_workplace/bin
This actually shouldn't be added to$PATH.
BTW, there are cases that $GOPATH only contains one directory, so that simply appending $GOPATH/bin will work.
I am looking for a solution that fits both cases. So, how do I write this in a bash config file?
linux bash shell
echo $GOPATH will print:
/mnt/star/program/go/package:/mnt/star/git_repository/workspace/go_workplace
There are 2 directories, I want to append the first directory's sub-directory bin/ to $PATH.
If I write $PATH=$PATH:$GOPATH/bin, then actually it appends 2 directories to $PATH:
/mnt/star/program/go/package
This only contains directories, it should be/mnt/star/program/go/package/bin./mnt/star/git_repository/workspace/go_workplace/bin
This actually shouldn't be added to$PATH.
BTW, there are cases that $GOPATH only contains one directory, so that simply appending $GOPATH/bin will work.
I am looking for a solution that fits both cases. So, how do I write this in a bash config file?
linux bash shell
linux bash shell
edited Feb 14 at 5:09
PRY
2,61431026
2,61431026
asked Feb 13 at 4:51
Eric WangEric Wang
20629
20629
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
Yes, that's exactly it! Only improvement this could get is default to~/goif not defined (which is default since Go 1.8 I believe) but this answers the asked question perfectly, thanks!
– filbranden
Feb 13 at 5:27
1
Works like a charm.
– Eric Wang
Feb 13 at 6:19
add a comment |
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.
It's the opposite... It's supposed to keep the first part, so you need to remove the last part. This is definitely the best answer, so if you fix it toPATH=$PATH:$GOPATH%%:*/binI'll definitely upvote it!
– filbranden
Feb 13 at 5:26
I read the question backwards. You're right, @filbranden. I'll fix it.
– Ken Jackson
Feb 13 at 5:29
add a comment |
Here’s an alternative answer using arrays; unfortunately this requires two steps to assign to the answer but I mention it anyway since it’s more general and potentially more versatile than the other answers:
IFS=: read -ra arr_gopath <<< "$GOPATH"
PATH="$PATH:$arr_gopath[0]/%//bin"
The advantage with this approach is that you can easily specify any component of the GOPATH, not just the first one. Or, to append all elements, followed by /bin, to PATH:
PATH="$PATH:$(IFS=:; echo "$arr_gopath[*]/%//bin")"
In both cases (splitting, joining) we set the internal field separator (IFS) to the colon character :. The substitution $var/%//bin appends /bin to the end of a chosen variable.
That said, if you wanted to append all GOPATH components, suffixed with /bin, to the PATH then the following would be simpler and doesn’t require arrays. It does, however, require that GOPATH is not empty:
PATH="$PATH:$GOPATH//://bin/bin"
($var//pattern/subst replaces all occurrences of pattern with subst, while $var/pattern/subst, used above, only replaces the first one.)
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500314%2fhow-do-i-add-the-bin-subdirectory-of-the-first-directory-in-gopath-to-path%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Yes, that's exactly it! Only improvement this could get is default to~/goif not defined (which is default since Go 1.8 I believe) but this answers the asked question perfectly, thanks!
– filbranden
Feb 13 at 5:27
1
Works like a charm.
– Eric Wang
Feb 13 at 6:19
add a comment |
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.
Yes, that's exactly it! Only improvement this could get is default to~/goif not defined (which is default since Go 1.8 I believe) but this answers the asked question perfectly, thanks!
– filbranden
Feb 13 at 5:27
1
Works like a charm.
– Eric Wang
Feb 13 at 6:19
add a comment |
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.
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.
edited Feb 13 at 13:50
psmears
44528
44528
answered Feb 13 at 5:26
PRYPRY
2,61431026
2,61431026
Yes, that's exactly it! Only improvement this could get is default to~/goif not defined (which is default since Go 1.8 I believe) but this answers the asked question perfectly, thanks!
– filbranden
Feb 13 at 5:27
1
Works like a charm.
– Eric Wang
Feb 13 at 6:19
add a comment |
Yes, that's exactly it! Only improvement this could get is default to~/goif not defined (which is default since Go 1.8 I believe) but this answers the asked question perfectly, thanks!
– filbranden
Feb 13 at 5:27
1
Works like a charm.
– Eric Wang
Feb 13 at 6:19
Yes, that's exactly it! Only improvement this could get is default to
~/go if not defined (which is default since Go 1.8 I believe) but this answers the asked question perfectly, thanks!– filbranden
Feb 13 at 5:27
Yes, that's exactly it! Only improvement this could get is default to
~/go if not defined (which is default since Go 1.8 I believe) but this answers the asked question perfectly, thanks!– filbranden
Feb 13 at 5:27
1
1
Works like a charm.
– Eric Wang
Feb 13 at 6:19
Works like a charm.
– Eric Wang
Feb 13 at 6:19
add a comment |
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.
It's the opposite... It's supposed to keep the first part, so you need to remove the last part. This is definitely the best answer, so if you fix it toPATH=$PATH:$GOPATH%%:*/binI'll definitely upvote it!
– filbranden
Feb 13 at 5:26
I read the question backwards. You're right, @filbranden. I'll fix it.
– Ken Jackson
Feb 13 at 5:29
add a comment |
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.
It's the opposite... It's supposed to keep the first part, so you need to remove the last part. This is definitely the best answer, so if you fix it toPATH=$PATH:$GOPATH%%:*/binI'll definitely upvote it!
– filbranden
Feb 13 at 5:26
I read the question backwards. You're right, @filbranden. I'll fix it.
– Ken Jackson
Feb 13 at 5:29
add a comment |
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.
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.
edited Feb 13 at 5:31
answered Feb 13 at 5:23
Ken JacksonKen Jackson
1063
1063
It's the opposite... It's supposed to keep the first part, so you need to remove the last part. This is definitely the best answer, so if you fix it toPATH=$PATH:$GOPATH%%:*/binI'll definitely upvote it!
– filbranden
Feb 13 at 5:26
I read the question backwards. You're right, @filbranden. I'll fix it.
– Ken Jackson
Feb 13 at 5:29
add a comment |
It's the opposite... It's supposed to keep the first part, so you need to remove the last part. This is definitely the best answer, so if you fix it toPATH=$PATH:$GOPATH%%:*/binI'll definitely upvote it!
– filbranden
Feb 13 at 5:26
I read the question backwards. You're right, @filbranden. I'll fix it.
– Ken Jackson
Feb 13 at 5:29
It's the opposite... It's supposed to keep the first part, so you need to remove the last part. This is definitely the best answer, so if you fix it to
PATH=$PATH:$GOPATH%%:*/bin I'll definitely upvote it!– filbranden
Feb 13 at 5:26
It's the opposite... It's supposed to keep the first part, so you need to remove the last part. This is definitely the best answer, so if you fix it to
PATH=$PATH:$GOPATH%%:*/bin I'll definitely upvote it!– filbranden
Feb 13 at 5:26
I read the question backwards. You're right, @filbranden. I'll fix it.
– Ken Jackson
Feb 13 at 5:29
I read the question backwards. You're right, @filbranden. I'll fix it.
– Ken Jackson
Feb 13 at 5:29
add a comment |
Here’s an alternative answer using arrays; unfortunately this requires two steps to assign to the answer but I mention it anyway since it’s more general and potentially more versatile than the other answers:
IFS=: read -ra arr_gopath <<< "$GOPATH"
PATH="$PATH:$arr_gopath[0]/%//bin"
The advantage with this approach is that you can easily specify any component of the GOPATH, not just the first one. Or, to append all elements, followed by /bin, to PATH:
PATH="$PATH:$(IFS=:; echo "$arr_gopath[*]/%//bin")"
In both cases (splitting, joining) we set the internal field separator (IFS) to the colon character :. The substitution $var/%//bin appends /bin to the end of a chosen variable.
That said, if you wanted to append all GOPATH components, suffixed with /bin, to the PATH then the following would be simpler and doesn’t require arrays. It does, however, require that GOPATH is not empty:
PATH="$PATH:$GOPATH//://bin/bin"
($var//pattern/subst replaces all occurrences of pattern with subst, while $var/pattern/subst, used above, only replaces the first one.)
add a comment |
Here’s an alternative answer using arrays; unfortunately this requires two steps to assign to the answer but I mention it anyway since it’s more general and potentially more versatile than the other answers:
IFS=: read -ra arr_gopath <<< "$GOPATH"
PATH="$PATH:$arr_gopath[0]/%//bin"
The advantage with this approach is that you can easily specify any component of the GOPATH, not just the first one. Or, to append all elements, followed by /bin, to PATH:
PATH="$PATH:$(IFS=:; echo "$arr_gopath[*]/%//bin")"
In both cases (splitting, joining) we set the internal field separator (IFS) to the colon character :. The substitution $var/%//bin appends /bin to the end of a chosen variable.
That said, if you wanted to append all GOPATH components, suffixed with /bin, to the PATH then the following would be simpler and doesn’t require arrays. It does, however, require that GOPATH is not empty:
PATH="$PATH:$GOPATH//://bin/bin"
($var//pattern/subst replaces all occurrences of pattern with subst, while $var/pattern/subst, used above, only replaces the first one.)
add a comment |
Here’s an alternative answer using arrays; unfortunately this requires two steps to assign to the answer but I mention it anyway since it’s more general and potentially more versatile than the other answers:
IFS=: read -ra arr_gopath <<< "$GOPATH"
PATH="$PATH:$arr_gopath[0]/%//bin"
The advantage with this approach is that you can easily specify any component of the GOPATH, not just the first one. Or, to append all elements, followed by /bin, to PATH:
PATH="$PATH:$(IFS=:; echo "$arr_gopath[*]/%//bin")"
In both cases (splitting, joining) we set the internal field separator (IFS) to the colon character :. The substitution $var/%//bin appends /bin to the end of a chosen variable.
That said, if you wanted to append all GOPATH components, suffixed with /bin, to the PATH then the following would be simpler and doesn’t require arrays. It does, however, require that GOPATH is not empty:
PATH="$PATH:$GOPATH//://bin/bin"
($var//pattern/subst replaces all occurrences of pattern with subst, while $var/pattern/subst, used above, only replaces the first one.)
Here’s an alternative answer using arrays; unfortunately this requires two steps to assign to the answer but I mention it anyway since it’s more general and potentially more versatile than the other answers:
IFS=: read -ra arr_gopath <<< "$GOPATH"
PATH="$PATH:$arr_gopath[0]/%//bin"
The advantage with this approach is that you can easily specify any component of the GOPATH, not just the first one. Or, to append all elements, followed by /bin, to PATH:
PATH="$PATH:$(IFS=:; echo "$arr_gopath[*]/%//bin")"
In both cases (splitting, joining) we set the internal field separator (IFS) to the colon character :. The substitution $var/%//bin appends /bin to the end of a chosen variable.
That said, if you wanted to append all GOPATH components, suffixed with /bin, to the PATH then the following would be simpler and doesn’t require arrays. It does, however, require that GOPATH is not empty:
PATH="$PATH:$GOPATH//://bin/bin"
($var//pattern/subst replaces all occurrences of pattern with subst, while $var/pattern/subst, used above, only replaces the first one.)
answered Feb 13 at 12:26
Konrad RudolphKonrad Rudolph
2,25231323
2,25231323
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500314%2fhow-do-i-add-the-bin-subdirectory-of-the-first-directory-in-gopath-to-path%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown