Best practice to use $? in bash?Bash: run command2 if command1 failsWhat is the meaning of $? in a shell...
Can humans ever directly see a few photons at a time? Can a human see a single photon?
When to remove insignificant variables?
Dates on degrees don’t make sense – will people care?
Cooling aquarium with aluminum heat sink
Am I legally required to provide a (GPL licensed) source code even after a project is abandoned?
How would modern naval warfare have to have developed differently for battleships to still be relevant in the 21st century?
What happened to Steve's Shield in Iron Man 2?
Can White Castle?
Shooting someone's past self using special relativity
Is a single radon daughter atom in air a solid?
Why are < or > required to use /dev/tcp
Why don't countries like Japan just print more money?
Data analysis internship not going well because of lack of programming background
What does it mean to not be able to take the derivative of a function multiple times?
How to remove this component from PCB
Why does Linux list NVMe drives as /dev/nvme0 instead of /dev/sda?
How do I professionally let my manager know I'll quit over an issue?
Explain why a line can never intersect a plane in exactly two points.
Understanding the reasoning of the woman who agreed with Shlomo to "cut the baby in half"
How to maintain a closed environment for one person for a long period of time
UK - Working without a contract. I resign and guy wants to sue me
If the Dragon's Breath spell is cast on a familiar, does it use the wizard's DC or familiar's DC?
How many people are necessary to maintain modern civilisation?
I found a password with hashcat, but it doesn't work
Best practice to use $? in bash?
Bash: run command2 if command1 failsWhat is the meaning of $? in a shell script?Shell script error handling while assigning STDOUT to variableRunning a bash script within a bash scriptUse git submodule foreach with functionPass a Function to another user in Bash?Bash Function DecoratorWhat is a best practice to represent a boolean value in a shell script?How can I find the program I'm hiding in bashBASH script best practiceHow to call bash function from within awk?Bash as float calculatorFailing on errors inside a function which is called on the left-hand-side of an && expression
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
When I read this answer about $? another question comes to mind.
Is there any best practice for how to use $? in bash?
Let's have a example:
We have a linear script and I we would like to know that all the command was executed ok.
Do you think it is ok to call a small function (let's call it "did_it_work"),
to check the error code and break if it's not.
#!/bin/bash
function did_it_work {
code=$1
if [ "$code" -ne "0" ]
then
echo "Error failure: code $code "
exit 1
fi
}
dir=some/path
mkdir -p $dir
did_it_work $?
cd $dir
did_it_work $?
run_some_command
did_it_work $?
This approach of course means that I have to manually solve the problem if there is any and rerun the script.
Do you think this is a good idea or is there some other best practice to do this?
/Thanks
shell bash error-handling
add a comment |
When I read this answer about $? another question comes to mind.
Is there any best practice for how to use $? in bash?
Let's have a example:
We have a linear script and I we would like to know that all the command was executed ok.
Do you think it is ok to call a small function (let's call it "did_it_work"),
to check the error code and break if it's not.
#!/bin/bash
function did_it_work {
code=$1
if [ "$code" -ne "0" ]
then
echo "Error failure: code $code "
exit 1
fi
}
dir=some/path
mkdir -p $dir
did_it_work $?
cd $dir
did_it_work $?
run_some_command
did_it_work $?
This approach of course means that I have to manually solve the problem if there is any and rerun the script.
Do you think this is a good idea or is there some other best practice to do this?
/Thanks
shell bash error-handling
add a comment |
When I read this answer about $? another question comes to mind.
Is there any best practice for how to use $? in bash?
Let's have a example:
We have a linear script and I we would like to know that all the command was executed ok.
Do you think it is ok to call a small function (let's call it "did_it_work"),
to check the error code and break if it's not.
#!/bin/bash
function did_it_work {
code=$1
if [ "$code" -ne "0" ]
then
echo "Error failure: code $code "
exit 1
fi
}
dir=some/path
mkdir -p $dir
did_it_work $?
cd $dir
did_it_work $?
run_some_command
did_it_work $?
This approach of course means that I have to manually solve the problem if there is any and rerun the script.
Do you think this is a good idea or is there some other best practice to do this?
/Thanks
shell bash error-handling
When I read this answer about $? another question comes to mind.
Is there any best practice for how to use $? in bash?
Let's have a example:
We have a linear script and I we would like to know that all the command was executed ok.
Do you think it is ok to call a small function (let's call it "did_it_work"),
to check the error code and break if it's not.
#!/bin/bash
function did_it_work {
code=$1
if [ "$code" -ne "0" ]
then
echo "Error failure: code $code "
exit 1
fi
}
dir=some/path
mkdir -p $dir
did_it_work $?
cd $dir
did_it_work $?
run_some_command
did_it_work $?
This approach of course means that I have to manually solve the problem if there is any and rerun the script.
Do you think this is a good idea or is there some other best practice to do this?
/Thanks
shell bash error-handling
shell bash error-handling
edited Apr 13 '17 at 12:36
Community♦
1
1
asked Mar 7 '11 at 16:35
JohanJohan
3,38911830
3,38911830
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
One common way is:
die() {
echo "$*" 1>&2
exit 1
}
then you use it like this:
mkdir -p some/path || die "mkdir failed with status $?"
Or if you want it to include the exit status, you could change it to:
die() {
echo "FATAL ERROR: $* (status $?)" 1>&2
exit 1
}
and then using it is a bit easier:
mkdir -p some/path || die "mkdir failed"
Just in case you haven't seen command1 || command2
before, it runs command1
, and if command1
fails, it runs command2
.
So you can read it like "make the directory or die".
Your example would look like:
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"
Or you can align the dies
further on the right so that the main code is more obvious.
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"
Also, if you are going to use the name some/path
multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to.
dir=some/path
mkdir -p "$dir" || die "Cannot make $dir"
cd "$dir" || die "Cannot cd to $dir"
run_some_command || die "Cannot run some_command"
add a comment |
You could rewrite your code like this:
#!/bin/bash
function try {
"$@"
code=$?
if [ $code -ne 0 ]
then
echo "$1 did not work: exit status $code"
exit 1
fi
}
try mkdir -p some/path
try cd some/path
try run_some_command
If you don't actually need to log the error code, but just whether the command succeeded or not, you can shorten try()
further like so:
function try {
if ! "$@"
then
echo "$1 did not work"
exit 1
fi
}
You can also use the code in this format. <pre>function try {
– BillThor
Mar 7 '11 at 17:40
If you use this functionality inline and don't want to return from the true side you can replacereturn $?
with the:
builtin.
– BillThor
Mar 7 '11 at 17:50
Why give a version that doesn't work with spaces? Change$*
to"$@"
.
– Mikel
Mar 7 '11 at 19:49
I've fixed it for you.
– Mikel
Mar 7 '11 at 20:05
add a comment |
If you really want to exit
on an error and are using Bash, then you should also consider set -e
. From help set
:
-e Exit immediately if a command exits with a non-zero status.
This of course doesn't give you the flexibility of a did_it_work() function, but it is an easy way to make sure your bash script stops on an error without adding lots of calls to your new function.
set -e
is useful. There are some commands that return non-zero under normal circumstances (for instance,diff
). When I'm using set -e in a script where I'm expecting a nonzero return, I docommand || true
.
– Shawn J. Goff
Mar 7 '11 at 18:49
1
Furthermore, even if you useset -e
, you can set an “exception handler” to catch all errors withtrap did_it_work EXIT
.
– Gilles
Mar 8 '11 at 18:41
1
This is part of POSIX, not a bash specific feature. Use it but be aware of some pitfalls mywiki.wooledge.org/BashFAQ/105
– kmkaplan
Feb 5 '14 at 7:46
@ShawnJ.Goff I prefer to docommand && true
. Like that the return value is not changed.
– kmkaplan
Feb 5 '14 at 7:49
@kmkaplan Your last comment doesn't make any sense to me. The purpose ofcommand || true
is to preventset -e
from exiting the script ifcommand
returns a non-zero exit code. It changes the exit code because we need to. The only thingcommand && true
does is runtrue
(return a zero exit code) if `command succeeded (returned a zero exit code) - it's a complete no-op.
– tripleee
yesterday
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%2f8759%2fbest-practice-to-use-in-bash%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
One common way is:
die() {
echo "$*" 1>&2
exit 1
}
then you use it like this:
mkdir -p some/path || die "mkdir failed with status $?"
Or if you want it to include the exit status, you could change it to:
die() {
echo "FATAL ERROR: $* (status $?)" 1>&2
exit 1
}
and then using it is a bit easier:
mkdir -p some/path || die "mkdir failed"
Just in case you haven't seen command1 || command2
before, it runs command1
, and if command1
fails, it runs command2
.
So you can read it like "make the directory or die".
Your example would look like:
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"
Or you can align the dies
further on the right so that the main code is more obvious.
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"
Also, if you are going to use the name some/path
multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to.
dir=some/path
mkdir -p "$dir" || die "Cannot make $dir"
cd "$dir" || die "Cannot cd to $dir"
run_some_command || die "Cannot run some_command"
add a comment |
One common way is:
die() {
echo "$*" 1>&2
exit 1
}
then you use it like this:
mkdir -p some/path || die "mkdir failed with status $?"
Or if you want it to include the exit status, you could change it to:
die() {
echo "FATAL ERROR: $* (status $?)" 1>&2
exit 1
}
and then using it is a bit easier:
mkdir -p some/path || die "mkdir failed"
Just in case you haven't seen command1 || command2
before, it runs command1
, and if command1
fails, it runs command2
.
So you can read it like "make the directory or die".
Your example would look like:
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"
Or you can align the dies
further on the right so that the main code is more obvious.
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"
Also, if you are going to use the name some/path
multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to.
dir=some/path
mkdir -p "$dir" || die "Cannot make $dir"
cd "$dir" || die "Cannot cd to $dir"
run_some_command || die "Cannot run some_command"
add a comment |
One common way is:
die() {
echo "$*" 1>&2
exit 1
}
then you use it like this:
mkdir -p some/path || die "mkdir failed with status $?"
Or if you want it to include the exit status, you could change it to:
die() {
echo "FATAL ERROR: $* (status $?)" 1>&2
exit 1
}
and then using it is a bit easier:
mkdir -p some/path || die "mkdir failed"
Just in case you haven't seen command1 || command2
before, it runs command1
, and if command1
fails, it runs command2
.
So you can read it like "make the directory or die".
Your example would look like:
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"
Or you can align the dies
further on the right so that the main code is more obvious.
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"
Also, if you are going to use the name some/path
multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to.
dir=some/path
mkdir -p "$dir" || die "Cannot make $dir"
cd "$dir" || die "Cannot cd to $dir"
run_some_command || die "Cannot run some_command"
One common way is:
die() {
echo "$*" 1>&2
exit 1
}
then you use it like this:
mkdir -p some/path || die "mkdir failed with status $?"
Or if you want it to include the exit status, you could change it to:
die() {
echo "FATAL ERROR: $* (status $?)" 1>&2
exit 1
}
and then using it is a bit easier:
mkdir -p some/path || die "mkdir failed"
Just in case you haven't seen command1 || command2
before, it runs command1
, and if command1
fails, it runs command2
.
So you can read it like "make the directory or die".
Your example would look like:
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"
Or you can align the dies
further on the right so that the main code is more obvious.
mkdir -p some/path || die "mkdir failed"
cd some/path || die "cd failed"
run_some_command || die "some_command failed"
Also, if you are going to use the name some/path
multiple times, store it in a variable so you don't have to keep typing it, and can easily change it if you need to.
dir=some/path
mkdir -p "$dir" || die "Cannot make $dir"
cd "$dir" || die "Cannot cd to $dir"
run_some_command || die "Cannot run some_command"
edited 10 mins ago
answered Mar 7 '11 at 19:54
MikelMikel
41.1k10103128
41.1k10103128
add a comment |
add a comment |
You could rewrite your code like this:
#!/bin/bash
function try {
"$@"
code=$?
if [ $code -ne 0 ]
then
echo "$1 did not work: exit status $code"
exit 1
fi
}
try mkdir -p some/path
try cd some/path
try run_some_command
If you don't actually need to log the error code, but just whether the command succeeded or not, you can shorten try()
further like so:
function try {
if ! "$@"
then
echo "$1 did not work"
exit 1
fi
}
You can also use the code in this format. <pre>function try {
– BillThor
Mar 7 '11 at 17:40
If you use this functionality inline and don't want to return from the true side you can replacereturn $?
with the:
builtin.
– BillThor
Mar 7 '11 at 17:50
Why give a version that doesn't work with spaces? Change$*
to"$@"
.
– Mikel
Mar 7 '11 at 19:49
I've fixed it for you.
– Mikel
Mar 7 '11 at 20:05
add a comment |
You could rewrite your code like this:
#!/bin/bash
function try {
"$@"
code=$?
if [ $code -ne 0 ]
then
echo "$1 did not work: exit status $code"
exit 1
fi
}
try mkdir -p some/path
try cd some/path
try run_some_command
If you don't actually need to log the error code, but just whether the command succeeded or not, you can shorten try()
further like so:
function try {
if ! "$@"
then
echo "$1 did not work"
exit 1
fi
}
You can also use the code in this format. <pre>function try {
– BillThor
Mar 7 '11 at 17:40
If you use this functionality inline and don't want to return from the true side you can replacereturn $?
with the:
builtin.
– BillThor
Mar 7 '11 at 17:50
Why give a version that doesn't work with spaces? Change$*
to"$@"
.
– Mikel
Mar 7 '11 at 19:49
I've fixed it for you.
– Mikel
Mar 7 '11 at 20:05
add a comment |
You could rewrite your code like this:
#!/bin/bash
function try {
"$@"
code=$?
if [ $code -ne 0 ]
then
echo "$1 did not work: exit status $code"
exit 1
fi
}
try mkdir -p some/path
try cd some/path
try run_some_command
If you don't actually need to log the error code, but just whether the command succeeded or not, you can shorten try()
further like so:
function try {
if ! "$@"
then
echo "$1 did not work"
exit 1
fi
}
You could rewrite your code like this:
#!/bin/bash
function try {
"$@"
code=$?
if [ $code -ne 0 ]
then
echo "$1 did not work: exit status $code"
exit 1
fi
}
try mkdir -p some/path
try cd some/path
try run_some_command
If you don't actually need to log the error code, but just whether the command succeeded or not, you can shorten try()
further like so:
function try {
if ! "$@"
then
echo "$1 did not work"
exit 1
fi
}
edited Mar 7 '11 at 20:12
answered Mar 7 '11 at 16:47
Warren YoungWarren Young
57.1k11145150
57.1k11145150
You can also use the code in this format. <pre>function try {
– BillThor
Mar 7 '11 at 17:40
If you use this functionality inline and don't want to return from the true side you can replacereturn $?
with the:
builtin.
– BillThor
Mar 7 '11 at 17:50
Why give a version that doesn't work with spaces? Change$*
to"$@"
.
– Mikel
Mar 7 '11 at 19:49
I've fixed it for you.
– Mikel
Mar 7 '11 at 20:05
add a comment |
You can also use the code in this format. <pre>function try {
– BillThor
Mar 7 '11 at 17:40
If you use this functionality inline and don't want to return from the true side you can replacereturn $?
with the:
builtin.
– BillThor
Mar 7 '11 at 17:50
Why give a version that doesn't work with spaces? Change$*
to"$@"
.
– Mikel
Mar 7 '11 at 19:49
I've fixed it for you.
– Mikel
Mar 7 '11 at 20:05
You can also use the code in this format. <pre>function try {
– BillThor
Mar 7 '11 at 17:40
You can also use the code in this format. <pre>function try {
– BillThor
Mar 7 '11 at 17:40
If you use this functionality inline and don't want to return from the true side you can replace
return $?
with the :
builtin.– BillThor
Mar 7 '11 at 17:50
If you use this functionality inline and don't want to return from the true side you can replace
return $?
with the :
builtin.– BillThor
Mar 7 '11 at 17:50
Why give a version that doesn't work with spaces? Change
$*
to "$@"
.– Mikel
Mar 7 '11 at 19:49
Why give a version that doesn't work with spaces? Change
$*
to "$@"
.– Mikel
Mar 7 '11 at 19:49
I've fixed it for you.
– Mikel
Mar 7 '11 at 20:05
I've fixed it for you.
– Mikel
Mar 7 '11 at 20:05
add a comment |
If you really want to exit
on an error and are using Bash, then you should also consider set -e
. From help set
:
-e Exit immediately if a command exits with a non-zero status.
This of course doesn't give you the flexibility of a did_it_work() function, but it is an easy way to make sure your bash script stops on an error without adding lots of calls to your new function.
set -e
is useful. There are some commands that return non-zero under normal circumstances (for instance,diff
). When I'm using set -e in a script where I'm expecting a nonzero return, I docommand || true
.
– Shawn J. Goff
Mar 7 '11 at 18:49
1
Furthermore, even if you useset -e
, you can set an “exception handler” to catch all errors withtrap did_it_work EXIT
.
– Gilles
Mar 8 '11 at 18:41
1
This is part of POSIX, not a bash specific feature. Use it but be aware of some pitfalls mywiki.wooledge.org/BashFAQ/105
– kmkaplan
Feb 5 '14 at 7:46
@ShawnJ.Goff I prefer to docommand && true
. Like that the return value is not changed.
– kmkaplan
Feb 5 '14 at 7:49
@kmkaplan Your last comment doesn't make any sense to me. The purpose ofcommand || true
is to preventset -e
from exiting the script ifcommand
returns a non-zero exit code. It changes the exit code because we need to. The only thingcommand && true
does is runtrue
(return a zero exit code) if `command succeeded (returned a zero exit code) - it's a complete no-op.
– tripleee
yesterday
add a comment |
If you really want to exit
on an error and are using Bash, then you should also consider set -e
. From help set
:
-e Exit immediately if a command exits with a non-zero status.
This of course doesn't give you the flexibility of a did_it_work() function, but it is an easy way to make sure your bash script stops on an error without adding lots of calls to your new function.
set -e
is useful. There are some commands that return non-zero under normal circumstances (for instance,diff
). When I'm using set -e in a script where I'm expecting a nonzero return, I docommand || true
.
– Shawn J. Goff
Mar 7 '11 at 18:49
1
Furthermore, even if you useset -e
, you can set an “exception handler” to catch all errors withtrap did_it_work EXIT
.
– Gilles
Mar 8 '11 at 18:41
1
This is part of POSIX, not a bash specific feature. Use it but be aware of some pitfalls mywiki.wooledge.org/BashFAQ/105
– kmkaplan
Feb 5 '14 at 7:46
@ShawnJ.Goff I prefer to docommand && true
. Like that the return value is not changed.
– kmkaplan
Feb 5 '14 at 7:49
@kmkaplan Your last comment doesn't make any sense to me. The purpose ofcommand || true
is to preventset -e
from exiting the script ifcommand
returns a non-zero exit code. It changes the exit code because we need to. The only thingcommand && true
does is runtrue
(return a zero exit code) if `command succeeded (returned a zero exit code) - it's a complete no-op.
– tripleee
yesterday
add a comment |
If you really want to exit
on an error and are using Bash, then you should also consider set -e
. From help set
:
-e Exit immediately if a command exits with a non-zero status.
This of course doesn't give you the flexibility of a did_it_work() function, but it is an easy way to make sure your bash script stops on an error without adding lots of calls to your new function.
If you really want to exit
on an error and are using Bash, then you should also consider set -e
. From help set
:
-e Exit immediately if a command exits with a non-zero status.
This of course doesn't give you the flexibility of a did_it_work() function, but it is an easy way to make sure your bash script stops on an error without adding lots of calls to your new function.
answered Mar 7 '11 at 18:14
Steven DSteven D
33.6k899108
33.6k899108
set -e
is useful. There are some commands that return non-zero under normal circumstances (for instance,diff
). When I'm using set -e in a script where I'm expecting a nonzero return, I docommand || true
.
– Shawn J. Goff
Mar 7 '11 at 18:49
1
Furthermore, even if you useset -e
, you can set an “exception handler” to catch all errors withtrap did_it_work EXIT
.
– Gilles
Mar 8 '11 at 18:41
1
This is part of POSIX, not a bash specific feature. Use it but be aware of some pitfalls mywiki.wooledge.org/BashFAQ/105
– kmkaplan
Feb 5 '14 at 7:46
@ShawnJ.Goff I prefer to docommand && true
. Like that the return value is not changed.
– kmkaplan
Feb 5 '14 at 7:49
@kmkaplan Your last comment doesn't make any sense to me. The purpose ofcommand || true
is to preventset -e
from exiting the script ifcommand
returns a non-zero exit code. It changes the exit code because we need to. The only thingcommand && true
does is runtrue
(return a zero exit code) if `command succeeded (returned a zero exit code) - it's a complete no-op.
– tripleee
yesterday
add a comment |
set -e
is useful. There are some commands that return non-zero under normal circumstances (for instance,diff
). When I'm using set -e in a script where I'm expecting a nonzero return, I docommand || true
.
– Shawn J. Goff
Mar 7 '11 at 18:49
1
Furthermore, even if you useset -e
, you can set an “exception handler” to catch all errors withtrap did_it_work EXIT
.
– Gilles
Mar 8 '11 at 18:41
1
This is part of POSIX, not a bash specific feature. Use it but be aware of some pitfalls mywiki.wooledge.org/BashFAQ/105
– kmkaplan
Feb 5 '14 at 7:46
@ShawnJ.Goff I prefer to docommand && true
. Like that the return value is not changed.
– kmkaplan
Feb 5 '14 at 7:49
@kmkaplan Your last comment doesn't make any sense to me. The purpose ofcommand || true
is to preventset -e
from exiting the script ifcommand
returns a non-zero exit code. It changes the exit code because we need to. The only thingcommand && true
does is runtrue
(return a zero exit code) if `command succeeded (returned a zero exit code) - it's a complete no-op.
– tripleee
yesterday
set -e
is useful. There are some commands that return non-zero under normal circumstances (for instance, diff
). When I'm using set -e in a script where I'm expecting a nonzero return, I do command || true
.– Shawn J. Goff
Mar 7 '11 at 18:49
set -e
is useful. There are some commands that return non-zero under normal circumstances (for instance, diff
). When I'm using set -e in a script where I'm expecting a nonzero return, I do command || true
.– Shawn J. Goff
Mar 7 '11 at 18:49
1
1
Furthermore, even if you use
set -e
, you can set an “exception handler” to catch all errors with trap did_it_work EXIT
.– Gilles
Mar 8 '11 at 18:41
Furthermore, even if you use
set -e
, you can set an “exception handler” to catch all errors with trap did_it_work EXIT
.– Gilles
Mar 8 '11 at 18:41
1
1
This is part of POSIX, not a bash specific feature. Use it but be aware of some pitfalls mywiki.wooledge.org/BashFAQ/105
– kmkaplan
Feb 5 '14 at 7:46
This is part of POSIX, not a bash specific feature. Use it but be aware of some pitfalls mywiki.wooledge.org/BashFAQ/105
– kmkaplan
Feb 5 '14 at 7:46
@ShawnJ.Goff I prefer to do
command && true
. Like that the return value is not changed.– kmkaplan
Feb 5 '14 at 7:49
@ShawnJ.Goff I prefer to do
command && true
. Like that the return value is not changed.– kmkaplan
Feb 5 '14 at 7:49
@kmkaplan Your last comment doesn't make any sense to me. The purpose of
command || true
is to prevent set -e
from exiting the script if command
returns a non-zero exit code. It changes the exit code because we need to. The only thing command && true
does is run true
(return a zero exit code) if `command succeeded (returned a zero exit code) - it's a complete no-op.– tripleee
yesterday
@kmkaplan Your last comment doesn't make any sense to me. The purpose of
command || true
is to prevent set -e
from exiting the script if command
returns a non-zero exit code. It changes the exit code because we need to. The only thing command && true
does is run true
(return a zero exit code) if `command succeeded (returned a zero exit code) - it's a complete no-op.– tripleee
yesterday
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%2f8759%2fbest-practice-to-use-in-bash%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