How does one extract a command's exit status into a variable?
up vote
6
down vote
favorite
I started learning Bash a couple of days ago.
I'm trying to obtain an exit status of grep
expression into a variable like this:
check=grep -ci 'text' file.sh
and the output that I got is
No command '-ic' found
Should I do it with a pipe command?
bash shell command-line grep return-status
add a comment |
up vote
6
down vote
favorite
I started learning Bash a couple of days ago.
I'm trying to obtain an exit status of grep
expression into a variable like this:
check=grep -ci 'text' file.sh
and the output that I got is
No command '-ic' found
Should I do it with a pipe command?
bash shell command-line grep return-status
1
Look up command substitution.
– 123
Jul 7 '16 at 8:08
6
For exit status you can examine$?
right after command is finished.
– coffeMug
Jul 7 '16 at 8:16
and you can examine the PIPESTATUS array if you want the status of one of the commands in a pipeline.
– cas
Jul 7 '16 at 11:29
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I started learning Bash a couple of days ago.
I'm trying to obtain an exit status of grep
expression into a variable like this:
check=grep -ci 'text' file.sh
and the output that I got is
No command '-ic' found
Should I do it with a pipe command?
bash shell command-line grep return-status
I started learning Bash a couple of days ago.
I'm trying to obtain an exit status of grep
expression into a variable like this:
check=grep -ci 'text' file.sh
and the output that I got is
No command '-ic' found
Should I do it with a pipe command?
bash shell command-line grep return-status
bash shell command-line grep return-status
edited Feb 16 at 18:00
Curious
555
555
asked Jul 7 '16 at 8:07
omri gilhar
31112
31112
1
Look up command substitution.
– 123
Jul 7 '16 at 8:08
6
For exit status you can examine$?
right after command is finished.
– coffeMug
Jul 7 '16 at 8:16
and you can examine the PIPESTATUS array if you want the status of one of the commands in a pipeline.
– cas
Jul 7 '16 at 11:29
add a comment |
1
Look up command substitution.
– 123
Jul 7 '16 at 8:08
6
For exit status you can examine$?
right after command is finished.
– coffeMug
Jul 7 '16 at 8:16
and you can examine the PIPESTATUS array if you want the status of one of the commands in a pipeline.
– cas
Jul 7 '16 at 11:29
1
1
Look up command substitution.
– 123
Jul 7 '16 at 8:08
Look up command substitution.
– 123
Jul 7 '16 at 8:08
6
6
For exit status you can examine
$?
right after command is finished.– coffeMug
Jul 7 '16 at 8:16
For exit status you can examine
$?
right after command is finished.– coffeMug
Jul 7 '16 at 8:16
and you can examine the PIPESTATUS array if you want the status of one of the commands in a pipeline.
– cas
Jul 7 '16 at 11:29
and you can examine the PIPESTATUS array if you want the status of one of the commands in a pipeline.
– cas
Jul 7 '16 at 11:29
add a comment |
5 Answers
5
active
oldest
votes
up vote
17
down vote
The shell stores the exit value of most recently executed command in the variable ?
. You can assign its value to one of your own variables like this:
grep -i 'PATTERN' file
check=$?
If you want to act on this value, you may either use your check
variable:
if [ "$check" -eq 0 ]; then
# do things for success
else
# do other things for failure
fi
or you could skip using a separate variable and having to inspect $?
all together:
if grep -q -i 'pattern' file; then
# do things (pattern was found)
else
# do other things (pattern was not found)
fi
or, if you just want to "do things" when the pattern is not found:
if ! grep -q -i 'pattern' file; then
# do things (pattern was not found)
fi
Saving $?
into another variable is only ever needed if you need to use it later, when the value in $?
has been overwritten, as in
mkdir "$dir"
err=$?
if [ "$err" -ne 0 ] && [ ! -d "$dir" ]; then
printf 'Error creating %s (error code %d)n' "$dir" "$err" >&2
exit "$err"
fi
In the above code snippet, $?
will be overwritten by the result of the [ "$err" -ne 0 ] && [ ! -d "$dir" ]
test. Saving it here is really only necessary if we need to display it and use it with exit
.
add a comment |
up vote
2
down vote
Your question is unclear but based on the code you’ve submitted, it looks like you want the variable check
to store the exit status of the grep
command. The way to do this is to run
grep -ci 'text' file.sh
check=$?
When running a command from a shell, its exit status is made available through the special shell parameter, $?
.
This is documented by POSIX (the standard for Unix-like operating systems) in
its specification for the shell and the Bash implementation is documented under Special Parameters.
Since you’re a new learner, I’d strongly recommend that you start with a good book and/or online tutorial to get the basics. Recommendations of external resources are discouraged on Stack Exchange sites but I’d suggest Lhunath and GreyCat’s Bash Guide.
add a comment |
up vote
2
down vote
You've told bash to set the variable check=grep
in the environment it passes to the command
-ci 'text' file.sh
but ci
does not exist.
I believe you meant to enclose that command in back-ticks, or in parentheses preceded by a dollar sign, either of which would assign the count of how many lines 'text' was found on (case insensitively) in the file:
check=`grep -ci 'text' file.sh`
check=$(grep -ci 'text' file.sh)
Now $check
should be 0 if no matches, or positive if there were any matches.
add a comment |
up vote
0
down vote
Confused why using -c when checking the output? It's used to check how many times something is matched - not if it's successful or not.
-c, --count
Suppress normal output; instead print a count of matching lines
for each input file. With the -v, --invert-match option (see
below), count non-matching lines. (-c is specified by POSIX.)
but in this example
check="$(grep --silent -i 'text' file.sh; echo $?)"
It doesn't output anything except a exit code, which is then echoed. This is the output that the variable check uses. I also prefer it because it's a single line.
You can replace --silent with -q. I use it since you're not interested in the grep output, just whether it worked or not.
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit
immediately with zero status if any match is found, even if an
error was detected. Also see the -s or --no-messages option.
(-q is specified by POSIX.)
$ check=$(echo test | grep -qi test; echo $?) # check variable is now set
$ echo $check
0
$ check=$(echo null | grep -qi test; echo $?)
$ echo $check
1
add a comment |
up vote
0
down vote
The correct way to assign the output of grep command to a variable is as @monty-harder mentioned:
check=`grep -ci 'text' file.sh`
check=$(grep -ci 'text' file.sh)
Though to assign the exit status of this command to a variable you have to use the shell parameter $?
right after executing the command as shown below:
grep -ci 'text' file.sh
check=$?
echo $check
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
17
down vote
The shell stores the exit value of most recently executed command in the variable ?
. You can assign its value to one of your own variables like this:
grep -i 'PATTERN' file
check=$?
If you want to act on this value, you may either use your check
variable:
if [ "$check" -eq 0 ]; then
# do things for success
else
# do other things for failure
fi
or you could skip using a separate variable and having to inspect $?
all together:
if grep -q -i 'pattern' file; then
# do things (pattern was found)
else
# do other things (pattern was not found)
fi
or, if you just want to "do things" when the pattern is not found:
if ! grep -q -i 'pattern' file; then
# do things (pattern was not found)
fi
Saving $?
into another variable is only ever needed if you need to use it later, when the value in $?
has been overwritten, as in
mkdir "$dir"
err=$?
if [ "$err" -ne 0 ] && [ ! -d "$dir" ]; then
printf 'Error creating %s (error code %d)n' "$dir" "$err" >&2
exit "$err"
fi
In the above code snippet, $?
will be overwritten by the result of the [ "$err" -ne 0 ] && [ ! -d "$dir" ]
test. Saving it here is really only necessary if we need to display it and use it with exit
.
add a comment |
up vote
17
down vote
The shell stores the exit value of most recently executed command in the variable ?
. You can assign its value to one of your own variables like this:
grep -i 'PATTERN' file
check=$?
If you want to act on this value, you may either use your check
variable:
if [ "$check" -eq 0 ]; then
# do things for success
else
# do other things for failure
fi
or you could skip using a separate variable and having to inspect $?
all together:
if grep -q -i 'pattern' file; then
# do things (pattern was found)
else
# do other things (pattern was not found)
fi
or, if you just want to "do things" when the pattern is not found:
if ! grep -q -i 'pattern' file; then
# do things (pattern was not found)
fi
Saving $?
into another variable is only ever needed if you need to use it later, when the value in $?
has been overwritten, as in
mkdir "$dir"
err=$?
if [ "$err" -ne 0 ] && [ ! -d "$dir" ]; then
printf 'Error creating %s (error code %d)n' "$dir" "$err" >&2
exit "$err"
fi
In the above code snippet, $?
will be overwritten by the result of the [ "$err" -ne 0 ] && [ ! -d "$dir" ]
test. Saving it here is really only necessary if we need to display it and use it with exit
.
add a comment |
up vote
17
down vote
up vote
17
down vote
The shell stores the exit value of most recently executed command in the variable ?
. You can assign its value to one of your own variables like this:
grep -i 'PATTERN' file
check=$?
If you want to act on this value, you may either use your check
variable:
if [ "$check" -eq 0 ]; then
# do things for success
else
# do other things for failure
fi
or you could skip using a separate variable and having to inspect $?
all together:
if grep -q -i 'pattern' file; then
# do things (pattern was found)
else
# do other things (pattern was not found)
fi
or, if you just want to "do things" when the pattern is not found:
if ! grep -q -i 'pattern' file; then
# do things (pattern was not found)
fi
Saving $?
into another variable is only ever needed if you need to use it later, when the value in $?
has been overwritten, as in
mkdir "$dir"
err=$?
if [ "$err" -ne 0 ] && [ ! -d "$dir" ]; then
printf 'Error creating %s (error code %d)n' "$dir" "$err" >&2
exit "$err"
fi
In the above code snippet, $?
will be overwritten by the result of the [ "$err" -ne 0 ] && [ ! -d "$dir" ]
test. Saving it here is really only necessary if we need to display it and use it with exit
.
The shell stores the exit value of most recently executed command in the variable ?
. You can assign its value to one of your own variables like this:
grep -i 'PATTERN' file
check=$?
If you want to act on this value, you may either use your check
variable:
if [ "$check" -eq 0 ]; then
# do things for success
else
# do other things for failure
fi
or you could skip using a separate variable and having to inspect $?
all together:
if grep -q -i 'pattern' file; then
# do things (pattern was found)
else
# do other things (pattern was not found)
fi
or, if you just want to "do things" when the pattern is not found:
if ! grep -q -i 'pattern' file; then
# do things (pattern was not found)
fi
Saving $?
into another variable is only ever needed if you need to use it later, when the value in $?
has been overwritten, as in
mkdir "$dir"
err=$?
if [ "$err" -ne 0 ] && [ ! -d "$dir" ]; then
printf 'Error creating %s (error code %d)n' "$dir" "$err" >&2
exit "$err"
fi
In the above code snippet, $?
will be overwritten by the result of the [ "$err" -ne 0 ] && [ ! -d "$dir" ]
test. Saving it here is really only necessary if we need to display it and use it with exit
.
edited Nov 26 at 13:06
answered Jul 7 '16 at 8:43
Kusalananda
118k16223364
118k16223364
add a comment |
add a comment |
up vote
2
down vote
Your question is unclear but based on the code you’ve submitted, it looks like you want the variable check
to store the exit status of the grep
command. The way to do this is to run
grep -ci 'text' file.sh
check=$?
When running a command from a shell, its exit status is made available through the special shell parameter, $?
.
This is documented by POSIX (the standard for Unix-like operating systems) in
its specification for the shell and the Bash implementation is documented under Special Parameters.
Since you’re a new learner, I’d strongly recommend that you start with a good book and/or online tutorial to get the basics. Recommendations of external resources are discouraged on Stack Exchange sites but I’d suggest Lhunath and GreyCat’s Bash Guide.
add a comment |
up vote
2
down vote
Your question is unclear but based on the code you’ve submitted, it looks like you want the variable check
to store the exit status of the grep
command. The way to do this is to run
grep -ci 'text' file.sh
check=$?
When running a command from a shell, its exit status is made available through the special shell parameter, $?
.
This is documented by POSIX (the standard for Unix-like operating systems) in
its specification for the shell and the Bash implementation is documented under Special Parameters.
Since you’re a new learner, I’d strongly recommend that you start with a good book and/or online tutorial to get the basics. Recommendations of external resources are discouraged on Stack Exchange sites but I’d suggest Lhunath and GreyCat’s Bash Guide.
add a comment |
up vote
2
down vote
up vote
2
down vote
Your question is unclear but based on the code you’ve submitted, it looks like you want the variable check
to store the exit status of the grep
command. The way to do this is to run
grep -ci 'text' file.sh
check=$?
When running a command from a shell, its exit status is made available through the special shell parameter, $?
.
This is documented by POSIX (the standard for Unix-like operating systems) in
its specification for the shell and the Bash implementation is documented under Special Parameters.
Since you’re a new learner, I’d strongly recommend that you start with a good book and/or online tutorial to get the basics. Recommendations of external resources are discouraged on Stack Exchange sites but I’d suggest Lhunath and GreyCat’s Bash Guide.
Your question is unclear but based on the code you’ve submitted, it looks like you want the variable check
to store the exit status of the grep
command. The way to do this is to run
grep -ci 'text' file.sh
check=$?
When running a command from a shell, its exit status is made available through the special shell parameter, $?
.
This is documented by POSIX (the standard for Unix-like operating systems) in
its specification for the shell and the Bash implementation is documented under Special Parameters.
Since you’re a new learner, I’d strongly recommend that you start with a good book and/or online tutorial to get the basics. Recommendations of external resources are discouraged on Stack Exchange sites but I’d suggest Lhunath and GreyCat’s Bash Guide.
edited Jul 7 '16 at 8:47
answered Jul 7 '16 at 8:35
Anthony Geoghegan
7,48133954
7,48133954
add a comment |
add a comment |
up vote
2
down vote
You've told bash to set the variable check=grep
in the environment it passes to the command
-ci 'text' file.sh
but ci
does not exist.
I believe you meant to enclose that command in back-ticks, or in parentheses preceded by a dollar sign, either of which would assign the count of how many lines 'text' was found on (case insensitively) in the file:
check=`grep -ci 'text' file.sh`
check=$(grep -ci 'text' file.sh)
Now $check
should be 0 if no matches, or positive if there were any matches.
add a comment |
up vote
2
down vote
You've told bash to set the variable check=grep
in the environment it passes to the command
-ci 'text' file.sh
but ci
does not exist.
I believe you meant to enclose that command in back-ticks, or in parentheses preceded by a dollar sign, either of which would assign the count of how many lines 'text' was found on (case insensitively) in the file:
check=`grep -ci 'text' file.sh`
check=$(grep -ci 'text' file.sh)
Now $check
should be 0 if no matches, or positive if there were any matches.
add a comment |
up vote
2
down vote
up vote
2
down vote
You've told bash to set the variable check=grep
in the environment it passes to the command
-ci 'text' file.sh
but ci
does not exist.
I believe you meant to enclose that command in back-ticks, or in parentheses preceded by a dollar sign, either of which would assign the count of how many lines 'text' was found on (case insensitively) in the file:
check=`grep -ci 'text' file.sh`
check=$(grep -ci 'text' file.sh)
Now $check
should be 0 if no matches, or positive if there were any matches.
You've told bash to set the variable check=grep
in the environment it passes to the command
-ci 'text' file.sh
but ci
does not exist.
I believe you meant to enclose that command in back-ticks, or in parentheses preceded by a dollar sign, either of which would assign the count of how many lines 'text' was found on (case insensitively) in the file:
check=`grep -ci 'text' file.sh`
check=$(grep -ci 'text' file.sh)
Now $check
should be 0 if no matches, or positive if there were any matches.
answered Jul 7 '16 at 19:40
Monty Harder
22215
22215
add a comment |
add a comment |
up vote
0
down vote
Confused why using -c when checking the output? It's used to check how many times something is matched - not if it's successful or not.
-c, --count
Suppress normal output; instead print a count of matching lines
for each input file. With the -v, --invert-match option (see
below), count non-matching lines. (-c is specified by POSIX.)
but in this example
check="$(grep --silent -i 'text' file.sh; echo $?)"
It doesn't output anything except a exit code, which is then echoed. This is the output that the variable check uses. I also prefer it because it's a single line.
You can replace --silent with -q. I use it since you're not interested in the grep output, just whether it worked or not.
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit
immediately with zero status if any match is found, even if an
error was detected. Also see the -s or --no-messages option.
(-q is specified by POSIX.)
$ check=$(echo test | grep -qi test; echo $?) # check variable is now set
$ echo $check
0
$ check=$(echo null | grep -qi test; echo $?)
$ echo $check
1
add a comment |
up vote
0
down vote
Confused why using -c when checking the output? It's used to check how many times something is matched - not if it's successful or not.
-c, --count
Suppress normal output; instead print a count of matching lines
for each input file. With the -v, --invert-match option (see
below), count non-matching lines. (-c is specified by POSIX.)
but in this example
check="$(grep --silent -i 'text' file.sh; echo $?)"
It doesn't output anything except a exit code, which is then echoed. This is the output that the variable check uses. I also prefer it because it's a single line.
You can replace --silent with -q. I use it since you're not interested in the grep output, just whether it worked or not.
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit
immediately with zero status if any match is found, even if an
error was detected. Also see the -s or --no-messages option.
(-q is specified by POSIX.)
$ check=$(echo test | grep -qi test; echo $?) # check variable is now set
$ echo $check
0
$ check=$(echo null | grep -qi test; echo $?)
$ echo $check
1
add a comment |
up vote
0
down vote
up vote
0
down vote
Confused why using -c when checking the output? It's used to check how many times something is matched - not if it's successful or not.
-c, --count
Suppress normal output; instead print a count of matching lines
for each input file. With the -v, --invert-match option (see
below), count non-matching lines. (-c is specified by POSIX.)
but in this example
check="$(grep --silent -i 'text' file.sh; echo $?)"
It doesn't output anything except a exit code, which is then echoed. This is the output that the variable check uses. I also prefer it because it's a single line.
You can replace --silent with -q. I use it since you're not interested in the grep output, just whether it worked or not.
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit
immediately with zero status if any match is found, even if an
error was detected. Also see the -s or --no-messages option.
(-q is specified by POSIX.)
$ check=$(echo test | grep -qi test; echo $?) # check variable is now set
$ echo $check
0
$ check=$(echo null | grep -qi test; echo $?)
$ echo $check
1
Confused why using -c when checking the output? It's used to check how many times something is matched - not if it's successful or not.
-c, --count
Suppress normal output; instead print a count of matching lines
for each input file. With the -v, --invert-match option (see
below), count non-matching lines. (-c is specified by POSIX.)
but in this example
check="$(grep --silent -i 'text' file.sh; echo $?)"
It doesn't output anything except a exit code, which is then echoed. This is the output that the variable check uses. I also prefer it because it's a single line.
You can replace --silent with -q. I use it since you're not interested in the grep output, just whether it worked or not.
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit
immediately with zero status if any match is found, even if an
error was detected. Also see the -s or --no-messages option.
(-q is specified by POSIX.)
$ check=$(echo test | grep -qi test; echo $?) # check variable is now set
$ echo $check
0
$ check=$(echo null | grep -qi test; echo $?)
$ echo $check
1
answered Jul 8 '16 at 2:03
Miati
1,0972919
1,0972919
add a comment |
add a comment |
up vote
0
down vote
The correct way to assign the output of grep command to a variable is as @monty-harder mentioned:
check=`grep -ci 'text' file.sh`
check=$(grep -ci 'text' file.sh)
Though to assign the exit status of this command to a variable you have to use the shell parameter $?
right after executing the command as shown below:
grep -ci 'text' file.sh
check=$?
echo $check
add a comment |
up vote
0
down vote
The correct way to assign the output of grep command to a variable is as @monty-harder mentioned:
check=`grep -ci 'text' file.sh`
check=$(grep -ci 'text' file.sh)
Though to assign the exit status of this command to a variable you have to use the shell parameter $?
right after executing the command as shown below:
grep -ci 'text' file.sh
check=$?
echo $check
add a comment |
up vote
0
down vote
up vote
0
down vote
The correct way to assign the output of grep command to a variable is as @monty-harder mentioned:
check=`grep -ci 'text' file.sh`
check=$(grep -ci 'text' file.sh)
Though to assign the exit status of this command to a variable you have to use the shell parameter $?
right after executing the command as shown below:
grep -ci 'text' file.sh
check=$?
echo $check
The correct way to assign the output of grep command to a variable is as @monty-harder mentioned:
check=`grep -ci 'text' file.sh`
check=$(grep -ci 'text' file.sh)
Though to assign the exit status of this command to a variable you have to use the shell parameter $?
right after executing the command as shown below:
grep -ci 'text' file.sh
check=$?
echo $check
edited Feb 16 at 18:02
answered Feb 16 at 17:37
Curious
555
555
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.
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%2funix.stackexchange.com%2fquestions%2f294371%2fhow-does-one-extract-a-commands-exit-status-into-a-variable%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
Look up command substitution.
– 123
Jul 7 '16 at 8:08
6
For exit status you can examine
$?
right after command is finished.– coffeMug
Jul 7 '16 at 8:16
and you can examine the PIPESTATUS array if you want the status of one of the commands in a pipeline.
– cas
Jul 7 '16 at 11:29