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
2 days ago
1
Related, maybe a duplicate: How do I cd into a directory in the home folder?
– wjandrea
2 days ago
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 yesterday
wjandrea
7,73642258
7,73642258
asked 2 days ago
tREEs
18613
18613
2
Dont forget tocd || 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
add a comment |
2
Dont forget tocd || 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
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
New contributor
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
2 days ago
2
@KevinJohnson That's true, though I would consider that to be a bug infiletocall.sh
.
– kasperd
2 days ago
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
2 days ago
1
What about redefinitions of$USER
? Maybehomedir="$(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
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
2 days ago
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
New contributor
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
2 days ago
2
@KevinJohnson That's true, though I would consider that to be a bug infiletocall.sh
.
– kasperd
2 days ago
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
New contributor
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
2 days ago
2
@KevinJohnson That's true, though I would consider that to be a bug infiletocall.sh
.
– kasperd
2 days ago
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
New contributor
~
(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
New contributor
New contributor
answered 2 days ago
Aviendha
883
883
New contributor
New contributor
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
2 days ago
2
@KevinJohnson That's true, though I would consider that to be a bug infiletocall.sh
.
– kasperd
2 days ago
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
2 days ago
2
@KevinJohnson That's true, though I would consider that to be a bug infiletocall.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
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
2 days ago
1
What about redefinitions of$USER
? Maybehomedir="$(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
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
2 days ago
1
What about redefinitions of$USER
? Maybehomedir="$(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
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 yesterday
answered 2 days ago
waltinator
21.6k74169
21.6k74169
Lovely code, thankyou!
– tREEs
2 days ago
1
What about redefinitions of$USER
? Maybehomedir="$(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
add a comment |
Lovely code, thankyou!
– tREEs
2 days ago
1
What about redefinitions of$USER
? Maybehomedir="$(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
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
2 days ago
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
2 days ago
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 2 days ago
Soren A
3,2151824
3,2151824
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
2 days ago
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
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
add a comment |
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
2 days ago
1
Related, maybe a duplicate: How do I cd into a directory in the home folder?
– wjandrea
2 days ago