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?)
command-line bash home-directory
add a comment |
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?)
command-line bash home-directory
2
Dont forget tocd || 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
add a comment |
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?)
command-line bash home-directory
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
command-line bash home-directory
edited Nov 18 at 14:12
wjandrea
7,96042258
7,96042258
asked Nov 17 at 11:19
tREEs
30714
30714
2
Dont forget tocd || 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
add a comment |
2
Dont forget tocd || 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
add a comment |
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
2
Iffiletocall.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 infiletocall.sh
.
– kasperd
Nov 17 at 22:32
add a comment |
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"
Lovely code, thankyou!
– tREEs
Nov 17 at 16:15
1
What about redefinitions of$USER
? Maybehomedir="$(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
add a comment |
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.
5
Technically, it's the other way around -~
is a shortcut for$HOME
. If you setHOME
to something, then~
will take that value (test with(HOME=foo; echo ~)
for example).
– Aviendha
Nov 17 at 14:38
add a comment |
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
2
Iffiletocall.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 infiletocall.sh
.
– kasperd
Nov 17 at 22:32
add a comment |
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
2
Iffiletocall.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 infiletocall.sh
.
– kasperd
Nov 17 at 22:32
add a comment |
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
~
(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
answered Nov 17 at 11:30
Aviendha
1
1
2
Iffiletocall.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 infiletocall.sh
.
– kasperd
Nov 17 at 22:32
add a comment |
2
Iffiletocall.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 infiletocall.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
add a comment |
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"
Lovely code, thankyou!
– tREEs
Nov 17 at 16:15
1
What about redefinitions of$USER
? Maybehomedir="$(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
add a comment |
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"
Lovely code, thankyou!
– tREEs
Nov 17 at 16:15
1
What about redefinitions of$USER
? Maybehomedir="$(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
add a comment |
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"
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"
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
? Maybehomedir="$(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
add a comment |
Lovely code, thankyou!
– tREEs
Nov 17 at 16:15
1
What about redefinitions of$USER
? Maybehomedir="$(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
add a comment |
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.
5
Technically, it's the other way around -~
is a shortcut for$HOME
. If you setHOME
to something, then~
will take that value (test with(HOME=foo; echo ~)
for example).
– Aviendha
Nov 17 at 14:38
add a comment |
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.
5
Technically, it's the other way around -~
is a shortcut for$HOME
. If you setHOME
to something, then~
will take that value (test with(HOME=foo; echo ~)
for example).
– Aviendha
Nov 17 at 14:38
add a comment |
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.
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.
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 setHOME
to something, then~
will take that value (test with(HOME=foo; echo ~)
for example).
– Aviendha
Nov 17 at 14:38
add a comment |
5
Technically, it's the other way around -~
is a shortcut for$HOME
. If you setHOME
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
add a comment |
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.
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%2faskubuntu.com%2fquestions%2f1093698%2fproper-method-for-addressing-a-users-home-directory%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
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