Not finding bash commands
I was wonder with someone can help me:
if [ -z $1 ]; then
user=$(whoami)
else
if [ ! -d "/home/$1" ]; then
echo "Requested $1 user home directory doesn't exist."
exit 1
fi
user=$1
fi
I was studying some bash commands when I saw two commands: -z and -d. I know what they do (first check for blank variable and the second check for existence directory). My question is how I can find descriptions about these commands (i.g man page -d/-z). They can be only used with if-else statement?
bash command
|
show 2 more comments
I was wonder with someone can help me:
if [ -z $1 ]; then
user=$(whoami)
else
if [ ! -d "/home/$1" ]; then
echo "Requested $1 user home directory doesn't exist."
exit 1
fi
user=$1
fi
I was studying some bash commands when I saw two commands: -z and -d. I know what they do (first check for blank variable and the second check for existence directory). My question is how I can find descriptions about these commands (i.g man page -d/-z). They can be only used with if-else statement?
bash command
1
Sorry guys.. I just found the answer in the tutorial I was reading:man bash
– Pedro Gabriel Lima
Jun 1 '18 at 12:38
1
I suggest that you tryhelp test.
– fd0
Jun 1 '18 at 12:40
@fd0 thanks!help testis a summary for conditional expression!
– Pedro Gabriel Lima
Jun 1 '18 at 12:46
1
notice that they're associated with a[syntax...
– Jeff Schaller♦
Jun 1 '18 at 12:46
@JeffSchaller - that was my second question! It doesn't make sense use those commands without a if-else as they only return true or false.
– Pedro Gabriel Lima
Jun 1 '18 at 12:47
|
show 2 more comments
I was wonder with someone can help me:
if [ -z $1 ]; then
user=$(whoami)
else
if [ ! -d "/home/$1" ]; then
echo "Requested $1 user home directory doesn't exist."
exit 1
fi
user=$1
fi
I was studying some bash commands when I saw two commands: -z and -d. I know what they do (first check for blank variable and the second check for existence directory). My question is how I can find descriptions about these commands (i.g man page -d/-z). They can be only used with if-else statement?
bash command
I was wonder with someone can help me:
if [ -z $1 ]; then
user=$(whoami)
else
if [ ! -d "/home/$1" ]; then
echo "Requested $1 user home directory doesn't exist."
exit 1
fi
user=$1
fi
I was studying some bash commands when I saw two commands: -z and -d. I know what they do (first check for blank variable and the second check for existence directory). My question is how I can find descriptions about these commands (i.g man page -d/-z). They can be only used with if-else statement?
bash command
bash command
asked Jun 1 '18 at 12:36
Pedro Gabriel LimaPedro Gabriel Lima
1284
1284
1
Sorry guys.. I just found the answer in the tutorial I was reading:man bash
– Pedro Gabriel Lima
Jun 1 '18 at 12:38
1
I suggest that you tryhelp test.
– fd0
Jun 1 '18 at 12:40
@fd0 thanks!help testis a summary for conditional expression!
– Pedro Gabriel Lima
Jun 1 '18 at 12:46
1
notice that they're associated with a[syntax...
– Jeff Schaller♦
Jun 1 '18 at 12:46
@JeffSchaller - that was my second question! It doesn't make sense use those commands without a if-else as they only return true or false.
– Pedro Gabriel Lima
Jun 1 '18 at 12:47
|
show 2 more comments
1
Sorry guys.. I just found the answer in the tutorial I was reading:man bash
– Pedro Gabriel Lima
Jun 1 '18 at 12:38
1
I suggest that you tryhelp test.
– fd0
Jun 1 '18 at 12:40
@fd0 thanks!help testis a summary for conditional expression!
– Pedro Gabriel Lima
Jun 1 '18 at 12:46
1
notice that they're associated with a[syntax...
– Jeff Schaller♦
Jun 1 '18 at 12:46
@JeffSchaller - that was my second question! It doesn't make sense use those commands without a if-else as they only return true or false.
– Pedro Gabriel Lima
Jun 1 '18 at 12:47
1
1
Sorry guys.. I just found the answer in the tutorial I was reading:
man bash– Pedro Gabriel Lima
Jun 1 '18 at 12:38
Sorry guys.. I just found the answer in the tutorial I was reading:
man bash– Pedro Gabriel Lima
Jun 1 '18 at 12:38
1
1
I suggest that you try
help test .– fd0
Jun 1 '18 at 12:40
I suggest that you try
help test .– fd0
Jun 1 '18 at 12:40
@fd0 thanks!
help test is a summary for conditional expression!– Pedro Gabriel Lima
Jun 1 '18 at 12:46
@fd0 thanks!
help test is a summary for conditional expression!– Pedro Gabriel Lima
Jun 1 '18 at 12:46
1
1
notice that they're associated with a
[ syntax...– Jeff Schaller♦
Jun 1 '18 at 12:46
notice that they're associated with a
[ syntax...– Jeff Schaller♦
Jun 1 '18 at 12:46
@JeffSchaller - that was my second question! It doesn't make sense use those commands without a if-else as they only return true or false.
– Pedro Gabriel Lima
Jun 1 '18 at 12:47
@JeffSchaller - that was my second question! It doesn't make sense use those commands without a if-else as they only return true or false.
– Pedro Gabriel Lima
Jun 1 '18 at 12:47
|
show 2 more comments
1 Answer
1
active
oldest
votes
The -d and -z are not commands but options to the test and [ utilities. These utilities are built into bash and documented in the bash manual. These utilities and these flags also happens to be standardized by POSIX, so they are available in any POSIX shell, not just bash.
If you're in an interactive bash session, you may get documentation for the built-in variants of these utilities by typing help test (help [ works too, but its text just refers to the documentation for test).
man test and man [ should work too. These manuals describe the external utilities, probably /bin/test and /bin/[, not the ones you use by default in bash.
So for example,
! test -z "$dir" && test -d "$dir" && printf '%s is a directory' "$dir"
is exactly equivalent to
! [ -z "$dir" ] && [ -d "$dir" ] && printf '%s is a directory' "$dir"
or, if you will,
if ! test -z "$dir" && test -d "$dir"; then
printf '%s is a directory' "$dir"
fi
and
if ! [ -z "$dir" ] && [ -d "$dir" ]; then
printf '%s is a directory' "$dir"
fi
(! [ -z "$dir" ] would probably be more commonly written [ ! -z "$dir" ] or [ -n "$dir" ], and I've only used the -z test above because it was mentioned in the question, the -d test on an empty string would fail anyway).
See also:
- POSIX documentation for the
testutility
Is there a reason the explicitly point to an outdated version of the POSIX standard?
– schily
Jun 1 '18 at 14:08
@schily I had an old bookmark. The 2018 edition is mostly the same as the 2016 edition that I linked to (Wikipedia says it's "technically identical" to the 2016 edition) and the page I linked to is identical in both. I'll start using the 2018 edition from now on though.
– Kusalananda♦
Jun 1 '18 at 14:19
I know that test did not change since then, but there are a lot of other things that did change since then. Some of them have been changed in order to fix bugs in the standard text. So technically identical may still include differences in the text.
– schily
Jun 1 '18 at 14:48
@schily Is there anything else that you can suggest that would improve this answer?
– Kusalananda♦
Jun 1 '18 at 15:19
The answer is OK and if you remove the year substring in the POSIX URL, you would always get the latest documents under this URL.
– schily
Jun 1 '18 at 15:22
|
show 4 more comments
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%2f447309%2fnot-finding-bash-commands%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The -d and -z are not commands but options to the test and [ utilities. These utilities are built into bash and documented in the bash manual. These utilities and these flags also happens to be standardized by POSIX, so they are available in any POSIX shell, not just bash.
If you're in an interactive bash session, you may get documentation for the built-in variants of these utilities by typing help test (help [ works too, but its text just refers to the documentation for test).
man test and man [ should work too. These manuals describe the external utilities, probably /bin/test and /bin/[, not the ones you use by default in bash.
So for example,
! test -z "$dir" && test -d "$dir" && printf '%s is a directory' "$dir"
is exactly equivalent to
! [ -z "$dir" ] && [ -d "$dir" ] && printf '%s is a directory' "$dir"
or, if you will,
if ! test -z "$dir" && test -d "$dir"; then
printf '%s is a directory' "$dir"
fi
and
if ! [ -z "$dir" ] && [ -d "$dir" ]; then
printf '%s is a directory' "$dir"
fi
(! [ -z "$dir" ] would probably be more commonly written [ ! -z "$dir" ] or [ -n "$dir" ], and I've only used the -z test above because it was mentioned in the question, the -d test on an empty string would fail anyway).
See also:
- POSIX documentation for the
testutility
Is there a reason the explicitly point to an outdated version of the POSIX standard?
– schily
Jun 1 '18 at 14:08
@schily I had an old bookmark. The 2018 edition is mostly the same as the 2016 edition that I linked to (Wikipedia says it's "technically identical" to the 2016 edition) and the page I linked to is identical in both. I'll start using the 2018 edition from now on though.
– Kusalananda♦
Jun 1 '18 at 14:19
I know that test did not change since then, but there are a lot of other things that did change since then. Some of them have been changed in order to fix bugs in the standard text. So technically identical may still include differences in the text.
– schily
Jun 1 '18 at 14:48
@schily Is there anything else that you can suggest that would improve this answer?
– Kusalananda♦
Jun 1 '18 at 15:19
The answer is OK and if you remove the year substring in the POSIX URL, you would always get the latest documents under this URL.
– schily
Jun 1 '18 at 15:22
|
show 4 more comments
The -d and -z are not commands but options to the test and [ utilities. These utilities are built into bash and documented in the bash manual. These utilities and these flags also happens to be standardized by POSIX, so they are available in any POSIX shell, not just bash.
If you're in an interactive bash session, you may get documentation for the built-in variants of these utilities by typing help test (help [ works too, but its text just refers to the documentation for test).
man test and man [ should work too. These manuals describe the external utilities, probably /bin/test and /bin/[, not the ones you use by default in bash.
So for example,
! test -z "$dir" && test -d "$dir" && printf '%s is a directory' "$dir"
is exactly equivalent to
! [ -z "$dir" ] && [ -d "$dir" ] && printf '%s is a directory' "$dir"
or, if you will,
if ! test -z "$dir" && test -d "$dir"; then
printf '%s is a directory' "$dir"
fi
and
if ! [ -z "$dir" ] && [ -d "$dir" ]; then
printf '%s is a directory' "$dir"
fi
(! [ -z "$dir" ] would probably be more commonly written [ ! -z "$dir" ] or [ -n "$dir" ], and I've only used the -z test above because it was mentioned in the question, the -d test on an empty string would fail anyway).
See also:
- POSIX documentation for the
testutility
Is there a reason the explicitly point to an outdated version of the POSIX standard?
– schily
Jun 1 '18 at 14:08
@schily I had an old bookmark. The 2018 edition is mostly the same as the 2016 edition that I linked to (Wikipedia says it's "technically identical" to the 2016 edition) and the page I linked to is identical in both. I'll start using the 2018 edition from now on though.
– Kusalananda♦
Jun 1 '18 at 14:19
I know that test did not change since then, but there are a lot of other things that did change since then. Some of them have been changed in order to fix bugs in the standard text. So technically identical may still include differences in the text.
– schily
Jun 1 '18 at 14:48
@schily Is there anything else that you can suggest that would improve this answer?
– Kusalananda♦
Jun 1 '18 at 15:19
The answer is OK and if you remove the year substring in the POSIX URL, you would always get the latest documents under this URL.
– schily
Jun 1 '18 at 15:22
|
show 4 more comments
The -d and -z are not commands but options to the test and [ utilities. These utilities are built into bash and documented in the bash manual. These utilities and these flags also happens to be standardized by POSIX, so they are available in any POSIX shell, not just bash.
If you're in an interactive bash session, you may get documentation for the built-in variants of these utilities by typing help test (help [ works too, but its text just refers to the documentation for test).
man test and man [ should work too. These manuals describe the external utilities, probably /bin/test and /bin/[, not the ones you use by default in bash.
So for example,
! test -z "$dir" && test -d "$dir" && printf '%s is a directory' "$dir"
is exactly equivalent to
! [ -z "$dir" ] && [ -d "$dir" ] && printf '%s is a directory' "$dir"
or, if you will,
if ! test -z "$dir" && test -d "$dir"; then
printf '%s is a directory' "$dir"
fi
and
if ! [ -z "$dir" ] && [ -d "$dir" ]; then
printf '%s is a directory' "$dir"
fi
(! [ -z "$dir" ] would probably be more commonly written [ ! -z "$dir" ] or [ -n "$dir" ], and I've only used the -z test above because it was mentioned in the question, the -d test on an empty string would fail anyway).
See also:
- POSIX documentation for the
testutility
The -d and -z are not commands but options to the test and [ utilities. These utilities are built into bash and documented in the bash manual. These utilities and these flags also happens to be standardized by POSIX, so they are available in any POSIX shell, not just bash.
If you're in an interactive bash session, you may get documentation for the built-in variants of these utilities by typing help test (help [ works too, but its text just refers to the documentation for test).
man test and man [ should work too. These manuals describe the external utilities, probably /bin/test and /bin/[, not the ones you use by default in bash.
So for example,
! test -z "$dir" && test -d "$dir" && printf '%s is a directory' "$dir"
is exactly equivalent to
! [ -z "$dir" ] && [ -d "$dir" ] && printf '%s is a directory' "$dir"
or, if you will,
if ! test -z "$dir" && test -d "$dir"; then
printf '%s is a directory' "$dir"
fi
and
if ! [ -z "$dir" ] && [ -d "$dir" ]; then
printf '%s is a directory' "$dir"
fi
(! [ -z "$dir" ] would probably be more commonly written [ ! -z "$dir" ] or [ -n "$dir" ], and I've only used the -z test above because it was mentioned in the question, the -d test on an empty string would fail anyway).
See also:
- POSIX documentation for the
testutility
edited Mar 6 at 15:01
answered Jun 1 '18 at 13:31
Kusalananda♦Kusalananda
138k17258428
138k17258428
Is there a reason the explicitly point to an outdated version of the POSIX standard?
– schily
Jun 1 '18 at 14:08
@schily I had an old bookmark. The 2018 edition is mostly the same as the 2016 edition that I linked to (Wikipedia says it's "technically identical" to the 2016 edition) and the page I linked to is identical in both. I'll start using the 2018 edition from now on though.
– Kusalananda♦
Jun 1 '18 at 14:19
I know that test did not change since then, but there are a lot of other things that did change since then. Some of them have been changed in order to fix bugs in the standard text. So technically identical may still include differences in the text.
– schily
Jun 1 '18 at 14:48
@schily Is there anything else that you can suggest that would improve this answer?
– Kusalananda♦
Jun 1 '18 at 15:19
The answer is OK and if you remove the year substring in the POSIX URL, you would always get the latest documents under this URL.
– schily
Jun 1 '18 at 15:22
|
show 4 more comments
Is there a reason the explicitly point to an outdated version of the POSIX standard?
– schily
Jun 1 '18 at 14:08
@schily I had an old bookmark. The 2018 edition is mostly the same as the 2016 edition that I linked to (Wikipedia says it's "technically identical" to the 2016 edition) and the page I linked to is identical in both. I'll start using the 2018 edition from now on though.
– Kusalananda♦
Jun 1 '18 at 14:19
I know that test did not change since then, but there are a lot of other things that did change since then. Some of them have been changed in order to fix bugs in the standard text. So technically identical may still include differences in the text.
– schily
Jun 1 '18 at 14:48
@schily Is there anything else that you can suggest that would improve this answer?
– Kusalananda♦
Jun 1 '18 at 15:19
The answer is OK and if you remove the year substring in the POSIX URL, you would always get the latest documents under this URL.
– schily
Jun 1 '18 at 15:22
Is there a reason the explicitly point to an outdated version of the POSIX standard?
– schily
Jun 1 '18 at 14:08
Is there a reason the explicitly point to an outdated version of the POSIX standard?
– schily
Jun 1 '18 at 14:08
@schily I had an old bookmark. The 2018 edition is mostly the same as the 2016 edition that I linked to (Wikipedia says it's "technically identical" to the 2016 edition) and the page I linked to is identical in both. I'll start using the 2018 edition from now on though.
– Kusalananda♦
Jun 1 '18 at 14:19
@schily I had an old bookmark. The 2018 edition is mostly the same as the 2016 edition that I linked to (Wikipedia says it's "technically identical" to the 2016 edition) and the page I linked to is identical in both. I'll start using the 2018 edition from now on though.
– Kusalananda♦
Jun 1 '18 at 14:19
I know that test did not change since then, but there are a lot of other things that did change since then. Some of them have been changed in order to fix bugs in the standard text. So technically identical may still include differences in the text.
– schily
Jun 1 '18 at 14:48
I know that test did not change since then, but there are a lot of other things that did change since then. Some of them have been changed in order to fix bugs in the standard text. So technically identical may still include differences in the text.
– schily
Jun 1 '18 at 14:48
@schily Is there anything else that you can suggest that would improve this answer?
– Kusalananda♦
Jun 1 '18 at 15:19
@schily Is there anything else that you can suggest that would improve this answer?
– Kusalananda♦
Jun 1 '18 at 15:19
The answer is OK and if you remove the year substring in the POSIX URL, you would always get the latest documents under this URL.
– schily
Jun 1 '18 at 15:22
The answer is OK and if you remove the year substring in the POSIX URL, you would always get the latest documents under this URL.
– schily
Jun 1 '18 at 15:22
|
show 4 more comments
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%2f447309%2fnot-finding-bash-commands%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
1
Sorry guys.. I just found the answer in the tutorial I was reading:
man bash– Pedro Gabriel Lima
Jun 1 '18 at 12:38
1
I suggest that you try
help test.– fd0
Jun 1 '18 at 12:40
@fd0 thanks!
help testis a summary for conditional expression!– Pedro Gabriel Lima
Jun 1 '18 at 12:46
1
notice that they're associated with a
[syntax...– Jeff Schaller♦
Jun 1 '18 at 12:46
@JeffSchaller - that was my second question! It doesn't make sense use those commands without a if-else as they only return true or false.
– Pedro Gabriel Lima
Jun 1 '18 at 12:47