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
    Nov 17 at 19:12






  • 1




    Related, maybe a duplicate: How do I cd into a directory in the home folder?
    – wjandrea
    Nov 17 at 19:32















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
    Nov 17 at 19:12






  • 1




    Related, maybe a duplicate: How do I cd into a directory in the home folder?
    – wjandrea
    Nov 17 at 19:32













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 Nov 18 at 14:12









wjandrea

7,96042258




7,96042258










asked Nov 17 at 11:19









tREEs

30714




30714








  • 2




    Dont forget to cd || fail !
    – D. Ben Knoble
    Nov 17 at 19:12






  • 1




    Related, maybe a duplicate: How do I cd into a directory in the home folder?
    – wjandrea
    Nov 17 at 19:32














  • 2




    Dont forget to cd || fail !
    – D. Ben Knoble
    Nov 17 at 19:12






  • 1




    Related, maybe a duplicate: How do I cd into a directory in the home folder?
    – wjandrea
    Nov 17 at 19:32








2




2




Dont forget to cd || fail !
– D. Ben Knoble
Nov 17 at 19:12




Dont forget to cd || fail !
– D. Ben Knoble
Nov 17 at 19:12




1




1




Related, maybe a duplicate: How do I cd into a directory in the home folder?
– wjandrea
Nov 17 at 19:32




Related, maybe a duplicate: How do I cd into a directory in the home folder?
– wjandrea
Nov 17 at 19:32










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

















  • 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
    Nov 17 at 18:16






  • 2




    @KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
    – kasperd
    Nov 17 at 22:32


















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
    Nov 17 at 16:15






  • 1




    What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
    – wjandrea
    Nov 17 at 19:26






  • 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
    Nov 17 at 21:33












  • @FedericoPoloni For exactly that reason I voted on Aviendha's answer.
    – kasperd
    Nov 17 at 22:40


















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
    Nov 17 at 14:38











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

















  • 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
    Nov 17 at 18:16






  • 2




    @KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
    – kasperd
    Nov 17 at 22:32















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

















  • 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
    Nov 17 at 18:16






  • 2




    @KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
    – kasperd
    Nov 17 at 22:32













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












~ (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












share|improve this answer



share|improve this answer










answered Nov 17 at 11:30









Aviendha

1




1








  • 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
    Nov 17 at 18:16






  • 2




    @KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
    – kasperd
    Nov 17 at 22:32














  • 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
    Nov 17 at 18:16






  • 2




    @KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
    – kasperd
    Nov 17 at 22:32








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
Nov 17 at 18:16




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
Nov 17 at 18:16




2




2




@KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
– kasperd
Nov 17 at 22:32




@KevinJohnson That's true, though I would consider that to be a bug in filetocall.sh.
– kasperd
Nov 17 at 22:32












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
    Nov 17 at 16:15






  • 1




    What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
    – wjandrea
    Nov 17 at 19:26






  • 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
    Nov 17 at 21:33












  • @FedericoPoloni For exactly that reason I voted on Aviendha's answer.
    – kasperd
    Nov 17 at 22:40















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
    Nov 17 at 16:15






  • 1




    What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
    – wjandrea
    Nov 17 at 19:26






  • 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
    Nov 17 at 21:33












  • @FedericoPoloni For exactly that reason I voted on Aviendha's answer.
    – kasperd
    Nov 17 at 22:40













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 Nov 18 at 5:06

























answered Nov 17 at 13:57









waltinator

21.7k74169




21.7k74169












  • Lovely code, thankyou!
    – tREEs
    Nov 17 at 16:15






  • 1




    What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
    – wjandrea
    Nov 17 at 19:26






  • 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
    Nov 17 at 21:33












  • @FedericoPoloni For exactly that reason I voted on Aviendha's answer.
    – kasperd
    Nov 17 at 22:40


















  • Lovely code, thankyou!
    – tREEs
    Nov 17 at 16:15






  • 1




    What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
    – wjandrea
    Nov 17 at 19:26






  • 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
    Nov 17 at 21:33












  • @FedericoPoloni For exactly that reason I voted on Aviendha's answer.
    – kasperd
    Nov 17 at 22:40
















Lovely code, thankyou!
– tREEs
Nov 17 at 16:15




Lovely code, thankyou!
– tREEs
Nov 17 at 16:15




1




1




What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
– wjandrea
Nov 17 at 19:26




What about redefinitions of $USER? Maybe homedir="$(getent passwd -- "$(whoami)" | cut -d: -f6)" ?
– wjandrea
Nov 17 at 19:26




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
Nov 17 at 21:33






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
Nov 17 at 21:33














@FedericoPoloni For exactly that reason I voted on Aviendha's answer.
– kasperd
Nov 17 at 22:40




@FedericoPoloni For exactly that reason I voted on Aviendha's answer.
– kasperd
Nov 17 at 22:40










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
    Nov 17 at 14:38















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
    Nov 17 at 14:38













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 Nov 17 at 11:34









Soren A

3,2691824




3,2691824








  • 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
    Nov 17 at 14:38














  • 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
    Nov 17 at 14:38








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
Nov 17 at 14:38




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
Nov 17 at 14:38


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • 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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




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

AnyDesk - Fatal Program Failure

How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

QoS: MAC-Priority for clients behind a repeater