Proper method for addressing a user's home directory











up vote
2
down vote

favorite












I am making a dialog menu for an Ubuntu VPN that is calling up other scripts like this:



cd
cd myrepo/gui
./filetocall.sh


The first cd is to ensure the directory for the second cd is always home.



Is there a better method I can use to address this in one line? (Without specifically naming the user in the path, so it can be installed and used on a few devices?)










share|improve this question




















  • 2




    Dont forget to cd || fail !
    – D. Ben Knoble
    2 days ago






  • 1




    Related, maybe a duplicate: How do I cd into a directory in the home folder?
    – wjandrea
    2 days ago















up vote
2
down vote

favorite












I am making a dialog menu for an Ubuntu VPN that is calling up other scripts like this:



cd
cd myrepo/gui
./filetocall.sh


The first cd is to ensure the directory for the second cd is always home.



Is there a better method I can use to address this in one line? (Without specifically naming the user in the path, so it can be installed and used on a few devices?)










share|improve this question




















  • 2




    Dont forget to cd || fail !
    – D. Ben Knoble
    2 days ago






  • 1




    Related, maybe a duplicate: How do I cd into a directory in the home folder?
    – wjandrea
    2 days ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am making a dialog menu for an Ubuntu VPN that is calling up other scripts like this:



cd
cd myrepo/gui
./filetocall.sh


The first cd is to ensure the directory for the second cd is always home.



Is there a better method I can use to address this in one line? (Without specifically naming the user in the path, so it can be installed and used on a few devices?)










share|improve this question















I am making a dialog menu for an Ubuntu VPN that is calling up other scripts like this:



cd
cd myrepo/gui
./filetocall.sh


The first cd is to ensure the directory for the second cd is always home.



Is there a better method I can use to address this in one line? (Without specifically naming the user in the path, so it can be installed and used on a few devices?)







command-line bash home-directory






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









wjandrea

7,73642258




7,73642258










asked 2 days ago









tREEs

18613




18613








  • 2




    Dont forget to cd || fail !
    – D. Ben Knoble
    2 days ago






  • 1




    Related, maybe a duplicate: How do I cd into a directory in the home folder?
    – wjandrea
    2 days ago














  • 2




    Dont forget to cd || fail !
    – D. Ben Knoble
    2 days ago






  • 1




    Related, maybe a duplicate: How do I cd into a directory in the home folder?
    – wjandrea
    2 days ago








2




2




Dont forget to cd || fail !
– D. Ben Knoble
2 days ago




Dont forget to cd || fail !
– D. Ben Knoble
2 days ago




1




1




Related, maybe a duplicate: How do I cd into a directory in the home folder?
– wjandrea
2 days ago




Related, maybe a duplicate: How do I cd into a directory in the home folder?
– wjandrea
2 days ago










3 Answers
3






active

oldest

votes

















up vote
7
down vote



accepted










~ (tilde) or $HOME can be used for getting the current user's home directory, so you could do:



cd ~/myrepo/gui
cd "$HOME/myrepo/gui"


Or even execute it directly:



~/myrepo/gui/filetocall.sh
"$HOME"/myrepo/gui/filetocall.sh





share|improve this answer








New contributor




Aviendha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 2




    If filetocall.sh expects the CWD to be ~/myrepo/gui then executing it directly could cause issues. Doing the cd and the executable call in two steps would prevent that.
    – Kevin Johnson
    2 days ago






  • 2




    @KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
    – kasperd
    2 days ago


















up vote
8
down vote













Use the same method used by login, which avoids being fooled by redefinitions of $HOME:



homedir="$(getent passwd $( /usr/bin/id -u ) | cut -d: -f6)"
cd "$homedir"





share|improve this answer























  • Lovely code, thankyou!
    – tREEs
    2 days ago






  • 1




    What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
    – wjandrea
    2 days ago






  • 4




    Well, it's not like I redefine $HOME often, but when I do it, it's precisely because I want scripts like this one to use that directory instead...
    – Federico Poloni
    2 days ago












  • @FedericoPoloni For exactly that reason I voted on Aviendha's answer.
    – kasperd
    2 days ago


















up vote
4
down vote













cd ~/myrepo/gui will do the trick, or a little longer: cd $HOME/myrepo/gui.



~ is a shell shortcut for users home directory, $HOME is a variable set by th shell for the same.






share|improve this answer

















  • 5




    Technically, it's the other way around - ~ is a shortcut for $HOME. If you set HOME to something, then ~ will take that value (test with (HOME=foo; echo ~) for example).
    – Aviendha
    2 days ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1093698%2fproper-method-for-addressing-a-users-home-directory%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








up vote
7
down vote



accepted










~ (tilde) or $HOME can be used for getting the current user's home directory, so you could do:



cd ~/myrepo/gui
cd "$HOME/myrepo/gui"


Or even execute it directly:



~/myrepo/gui/filetocall.sh
"$HOME"/myrepo/gui/filetocall.sh





share|improve this answer








New contributor




Aviendha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 2




    If filetocall.sh expects the CWD to be ~/myrepo/gui then executing it directly could cause issues. Doing the cd and the executable call in two steps would prevent that.
    – Kevin Johnson
    2 days ago






  • 2




    @KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
    – kasperd
    2 days ago















up vote
7
down vote



accepted










~ (tilde) or $HOME can be used for getting the current user's home directory, so you could do:



cd ~/myrepo/gui
cd "$HOME/myrepo/gui"


Or even execute it directly:



~/myrepo/gui/filetocall.sh
"$HOME"/myrepo/gui/filetocall.sh





share|improve this answer








New contributor




Aviendha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 2




    If filetocall.sh expects the CWD to be ~/myrepo/gui then executing it directly could cause issues. Doing the cd and the executable call in two steps would prevent that.
    – Kevin Johnson
    2 days ago






  • 2




    @KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
    – kasperd
    2 days ago













up vote
7
down vote



accepted







up vote
7
down vote



accepted






~ (tilde) or $HOME can be used for getting the current user's home directory, so you could do:



cd ~/myrepo/gui
cd "$HOME/myrepo/gui"


Or even execute it directly:



~/myrepo/gui/filetocall.sh
"$HOME"/myrepo/gui/filetocall.sh





share|improve this answer








New contributor




Aviendha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









~ (tilde) or $HOME can be used for getting the current user's home directory, so you could do:



cd ~/myrepo/gui
cd "$HOME/myrepo/gui"


Or even execute it directly:



~/myrepo/gui/filetocall.sh
"$HOME"/myrepo/gui/filetocall.sh






share|improve this answer








New contributor




Aviendha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




Aviendha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered 2 days ago









Aviendha

883




883




New contributor




Aviendha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Aviendha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Aviendha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    If filetocall.sh expects the CWD to be ~/myrepo/gui then executing it directly could cause issues. Doing the cd and the executable call in two steps would prevent that.
    – Kevin Johnson
    2 days ago






  • 2




    @KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
    – kasperd
    2 days ago














  • 2




    If filetocall.sh expects the CWD to be ~/myrepo/gui then executing it directly could cause issues. Doing the cd and the executable call in two steps would prevent that.
    – Kevin Johnson
    2 days ago






  • 2




    @KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
    – kasperd
    2 days ago








2




2




If filetocall.sh expects the CWD to be ~/myrepo/gui then executing it directly could cause issues. Doing the cd and the executable call in two steps would prevent that.
– Kevin Johnson
2 days ago




If filetocall.sh expects the CWD to be ~/myrepo/gui then executing it directly could cause issues. Doing the cd and the executable call in two steps would prevent that.
– Kevin Johnson
2 days ago




2




2




@KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
– kasperd
2 days ago




@KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
– kasperd
2 days ago












up vote
8
down vote













Use the same method used by login, which avoids being fooled by redefinitions of $HOME:



homedir="$(getent passwd $( /usr/bin/id -u ) | cut -d: -f6)"
cd "$homedir"





share|improve this answer























  • Lovely code, thankyou!
    – tREEs
    2 days ago






  • 1




    What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
    – wjandrea
    2 days ago






  • 4




    Well, it's not like I redefine $HOME often, but when I do it, it's precisely because I want scripts like this one to use that directory instead...
    – Federico Poloni
    2 days ago












  • @FedericoPoloni For exactly that reason I voted on Aviendha's answer.
    – kasperd
    2 days ago















up vote
8
down vote













Use the same method used by login, which avoids being fooled by redefinitions of $HOME:



homedir="$(getent passwd $( /usr/bin/id -u ) | cut -d: -f6)"
cd "$homedir"





share|improve this answer























  • Lovely code, thankyou!
    – tREEs
    2 days ago






  • 1




    What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
    – wjandrea
    2 days ago






  • 4




    Well, it's not like I redefine $HOME often, but when I do it, it's precisely because I want scripts like this one to use that directory instead...
    – Federico Poloni
    2 days ago












  • @FedericoPoloni For exactly that reason I voted on Aviendha's answer.
    – kasperd
    2 days ago













up vote
8
down vote










up vote
8
down vote









Use the same method used by login, which avoids being fooled by redefinitions of $HOME:



homedir="$(getent passwd $( /usr/bin/id -u ) | cut -d: -f6)"
cd "$homedir"





share|improve this answer














Use the same method used by login, which avoids being fooled by redefinitions of $HOME:



homedir="$(getent passwd $( /usr/bin/id -u ) | cut -d: -f6)"
cd "$homedir"






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









waltinator

21.6k74169




21.6k74169












  • Lovely code, thankyou!
    – tREEs
    2 days ago






  • 1




    What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
    – wjandrea
    2 days ago






  • 4




    Well, it's not like I redefine $HOME often, but when I do it, it's precisely because I want scripts like this one to use that directory instead...
    – Federico Poloni
    2 days ago












  • @FedericoPoloni For exactly that reason I voted on Aviendha's answer.
    – kasperd
    2 days ago


















  • Lovely code, thankyou!
    – tREEs
    2 days ago






  • 1




    What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
    – wjandrea
    2 days ago






  • 4




    Well, it's not like I redefine $HOME often, but when I do it, it's precisely because I want scripts like this one to use that directory instead...
    – Federico Poloni
    2 days ago












  • @FedericoPoloni For exactly that reason I voted on Aviendha's answer.
    – kasperd
    2 days ago
















Lovely code, thankyou!
– tREEs
2 days ago




Lovely code, thankyou!
– tREEs
2 days ago




1




1




What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
– wjandrea
2 days ago




What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
– wjandrea
2 days ago




4




4




Well, it's not like I redefine $HOME often, but when I do it, it's precisely because I want scripts like this one to use that directory instead...
– Federico Poloni
2 days ago






Well, it's not like I redefine $HOME often, but when I do it, it's precisely because I want scripts like this one to use that directory instead...
– Federico Poloni
2 days ago














@FedericoPoloni For exactly that reason I voted on Aviendha's answer.
– kasperd
2 days ago




@FedericoPoloni For exactly that reason I voted on Aviendha's answer.
– kasperd
2 days ago










up vote
4
down vote













cd ~/myrepo/gui will do the trick, or a little longer: cd $HOME/myrepo/gui.



~ is a shell shortcut for users home directory, $HOME is a variable set by th shell for the same.






share|improve this answer

















  • 5




    Technically, it's the other way around - ~ is a shortcut for $HOME. If you set HOME to something, then ~ will take that value (test with (HOME=foo; echo ~) for example).
    – Aviendha
    2 days ago















up vote
4
down vote













cd ~/myrepo/gui will do the trick, or a little longer: cd $HOME/myrepo/gui.



~ is a shell shortcut for users home directory, $HOME is a variable set by th shell for the same.






share|improve this answer

















  • 5




    Technically, it's the other way around - ~ is a shortcut for $HOME. If you set HOME to something, then ~ will take that value (test with (HOME=foo; echo ~) for example).
    – Aviendha
    2 days ago













up vote
4
down vote










up vote
4
down vote









cd ~/myrepo/gui will do the trick, or a little longer: cd $HOME/myrepo/gui.



~ is a shell shortcut for users home directory, $HOME is a variable set by th shell for the same.






share|improve this answer












cd ~/myrepo/gui will do the trick, or a little longer: cd $HOME/myrepo/gui.



~ is a shell shortcut for users home directory, $HOME is a variable set by th shell for the same.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Soren A

3,2151824




3,2151824








  • 5




    Technically, it's the other way around - ~ is a shortcut for $HOME. If you set HOME to something, then ~ will take that value (test with (HOME=foo; echo ~) for example).
    – Aviendha
    2 days ago














  • 5




    Technically, it's the other way around - ~ is a shortcut for $HOME. If you set HOME to something, then ~ will take that value (test with (HOME=foo; echo ~) for example).
    – Aviendha
    2 days ago








5




5




Technically, it's the other way around - ~ is a shortcut for $HOME. If you set HOME to something, then ~ will take that value (test with (HOME=foo; echo ~) for example).
– Aviendha
2 days ago




Technically, it's the other way around - ~ is a shortcut for $HOME. If you set HOME to something, then ~ will take that value (test with (HOME=foo; echo ~) for example).
– Aviendha
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1093698%2fproper-method-for-addressing-a-users-home-directory%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy