Print condition result directly in bash without using if
Let's say I have this simple code:
echo "Are there any arguments?"
if [ $# -eq 0 ]; then
echo "false"
else
echo "true"
fi
As you can see it would be better to just have opportunity to directly print condition result, but I don't know how to do it.
It would be something like:
echo "$([ $# -eq 0 ])"
But it doesn't work that way. Can we do this without if?
bash shell test
add a comment |
Let's say I have this simple code:
echo "Are there any arguments?"
if [ $# -eq 0 ]; then
echo "false"
else
echo "true"
fi
As you can see it would be better to just have opportunity to directly print condition result, but I don't know how to do it.
It would be something like:
echo "$([ $# -eq 0 ])"
But it doesn't work that way. Can we do this without if?
bash shell test
You can try zsh. Zsh will change PS1 color (command prompt color) according to$?(the return value of the last command). imgur.com/a/yBz2H
– Weekend
Aug 10 '17 at 16:04
add a comment |
Let's say I have this simple code:
echo "Are there any arguments?"
if [ $# -eq 0 ]; then
echo "false"
else
echo "true"
fi
As you can see it would be better to just have opportunity to directly print condition result, but I don't know how to do it.
It would be something like:
echo "$([ $# -eq 0 ])"
But it doesn't work that way. Can we do this without if?
bash shell test
Let's say I have this simple code:
echo "Are there any arguments?"
if [ $# -eq 0 ]; then
echo "false"
else
echo "true"
fi
As you can see it would be better to just have opportunity to directly print condition result, but I don't know how to do it.
It would be something like:
echo "$([ $# -eq 0 ])"
But it doesn't work that way. Can we do this without if?
bash shell test
bash shell test
edited Oct 15 '15 at 22:50
Gilles
538k12810891606
538k12810891606
asked Oct 15 '15 at 19:34
ctomekctomek
12015
12015
You can try zsh. Zsh will change PS1 color (command prompt color) according to$?(the return value of the last command). imgur.com/a/yBz2H
– Weekend
Aug 10 '17 at 16:04
add a comment |
You can try zsh. Zsh will change PS1 color (command prompt color) according to$?(the return value of the last command). imgur.com/a/yBz2H
– Weekend
Aug 10 '17 at 16:04
You can try zsh. Zsh will change PS1 color (command prompt color) according to
$?(the return value of the last command). imgur.com/a/yBz2H– Weekend
Aug 10 '17 at 16:04
You can try zsh. Zsh will change PS1 color (command prompt color) according to
$?(the return value of the last command). imgur.com/a/yBz2H– Weekend
Aug 10 '17 at 16:04
add a comment |
3 Answers
3
active
oldest
votes
You can use the list control operators && and || instead:
[[ $# -eq 0 ]] && { echo false; } || { echo true; }
The { } group a list of commands, you don't need them for just a single command, but they often make such constructions more readable.
add a comment |
You can use $? that keeps the exit code of the last executed command:
echo "Are there any arguments?"
[ $# -eq 0 ]
echo $?
add a comment |
Since I can't post comments yet (insufficient rep), I'll point out in a new answer that mr-spuratic's solution also works with single brackets, like so:
[ $# -eq 0 ] && { echo false; } || { echo true; }
add a comment |
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%2f236487%2fprint-condition-result-directly-in-bash-without-using-if%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
You can use the list control operators && and || instead:
[[ $# -eq 0 ]] && { echo false; } || { echo true; }
The { } group a list of commands, you don't need them for just a single command, but they often make such constructions more readable.
add a comment |
You can use the list control operators && and || instead:
[[ $# -eq 0 ]] && { echo false; } || { echo true; }
The { } group a list of commands, you don't need them for just a single command, but they often make such constructions more readable.
add a comment |
You can use the list control operators && and || instead:
[[ $# -eq 0 ]] && { echo false; } || { echo true; }
The { } group a list of commands, you don't need them for just a single command, but they often make such constructions more readable.
You can use the list control operators && and || instead:
[[ $# -eq 0 ]] && { echo false; } || { echo true; }
The { } group a list of commands, you don't need them for just a single command, but they often make such constructions more readable.
answered Oct 15 '15 at 19:46
mr.spuraticmr.spuratic
6,9811128
6,9811128
add a comment |
add a comment |
You can use $? that keeps the exit code of the last executed command:
echo "Are there any arguments?"
[ $# -eq 0 ]
echo $?
add a comment |
You can use $? that keeps the exit code of the last executed command:
echo "Are there any arguments?"
[ $# -eq 0 ]
echo $?
add a comment |
You can use $? that keeps the exit code of the last executed command:
echo "Are there any arguments?"
[ $# -eq 0 ]
echo $?
You can use $? that keeps the exit code of the last executed command:
echo "Are there any arguments?"
[ $# -eq 0 ]
echo $?
answered Oct 15 '15 at 19:46
chorobachoroba
26.7k44975
26.7k44975
add a comment |
add a comment |
Since I can't post comments yet (insufficient rep), I'll point out in a new answer that mr-spuratic's solution also works with single brackets, like so:
[ $# -eq 0 ] && { echo false; } || { echo true; }
add a comment |
Since I can't post comments yet (insufficient rep), I'll point out in a new answer that mr-spuratic's solution also works with single brackets, like so:
[ $# -eq 0 ] && { echo false; } || { echo true; }
add a comment |
Since I can't post comments yet (insufficient rep), I'll point out in a new answer that mr-spuratic's solution also works with single brackets, like so:
[ $# -eq 0 ] && { echo false; } || { echo true; }
Since I can't post comments yet (insufficient rep), I'll point out in a new answer that mr-spuratic's solution also works with single brackets, like so:
[ $# -eq 0 ] && { echo false; } || { echo true; }
answered 4 mins ago
MrPotatoHeadMrPotatoHead
11
11
add a comment |
add a comment |
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%2f236487%2fprint-condition-result-directly-in-bash-without-using-if%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
You can try zsh. Zsh will change PS1 color (command prompt color) according to
$?(the return value of the last command). imgur.com/a/yBz2H– Weekend
Aug 10 '17 at 16:04