Timeline for Difference between if [ ... and test ... statement in bash
Current License: CC BY-SA 4.0
22 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Oct 27, 2021 at 12:53 | vote | accept | fuumind | ||
S Oct 12, 2021 at 8:59 | history | suggested | fuumind | CC BY-SA 4.0 |
Made it clearer that every function returns "$?" at the end, not just this function.
|
Oct 12, 2021 at 8:30 | review | Suggested edits | |||
S Oct 12, 2021 at 8:59 | |||||
Oct 4, 2021 at 17:24 | comment | added | ilkkachu | @they, yeah. I think I just find the behaviour a bit odd, i.e. why would it not just leave the exit status of the last command in effect, regardless of if it was in a condition or in the inner branches. There's probably a sensible reason, but in a way it warrants a comment in my mind... | |
S Oct 4, 2021 at 15:35 | history | edited | Kusalananda♦ | CC BY-SA 4.0 |
Made it clearer that the exit status of both if-statements is zero when $m is non-zero,
|
S Oct 4, 2021 at 15:35 | history | suggested | fuumind | CC BY-SA 4.0 |
Made it clearer that the exit status of both if-statements is zero when $m is non-zero,
|
Oct 4, 2021 at 15:30 | review | Suggested edits | |||
S Oct 4, 2021 at 15:35 | |||||
Oct 4, 2021 at 15:28 | history | edited | Kusalananda♦ | CC BY-SA 4.0 |
deleted 168 characters in body
|
Oct 4, 2021 at 15:28 | comment | added | Kusalananda♦ | @StéphaneChazelas Sorry, I totally messed that up. Too late in the day to be doing this. Thanks for the heads-up. | |
Oct 4, 2021 at 15:25 | comment | added | Stéphane Chazelas |
Not sure what you mean by For an empty pattern, this would return false (non-zero) if the file was empty. Testing with -z on the output from grep would in that case return true (zero).. printf '' | grep -qF '' would return false and so would [ -n "$(printf '' | grep -F '')" ] . OTOH (and it's what I meant above), printf '\n\n\n\n\n' | grep -qF '' would return true, but [ -n "$(printf '\n\n\n\n\n' | grep -F '')" ] would return false as command substitution strips all trailing newline characters.
|
|
Oct 4, 2021 at 15:21 | history | edited | Kusalananda♦ | CC BY-SA 4.0 |
added 154 characters in body
|
Oct 4, 2021 at 15:17 | comment | added | fuumind |
I have edited the question to make $m a local variable.
|
|
Oct 4, 2021 at 15:15 | history | edited | Kusalananda♦ | CC BY-SA 4.0 |
deleted 8 characters in body
|
Oct 4, 2021 at 15:15 | comment | added | Stéphane Chazelas |
Or is_match() { m=$(grep -F -- "$1" file.txt); } if we do want to check if there was a match and store the matching lines (though not the empty trailing ones) in $m . $m could be empty despite a match if all the matching lines are empty or with some shells if the first matching line starts with a NUL byte.
|
|
Oct 4, 2021 at 15:13 | history | edited | Kusalananda♦ | CC BY-SA 4.0 |
added 295 characters in body
|
Oct 4, 2021 at 15:08 | comment | added | Stéphane Chazelas |
Also is_match should probably just be is_match() { grep -qFe "$1" file.txt; } or is_match() { grep -qF -- "$1" file.txt; } , unless you do also want to store the matching line in $m (but then that would be poorly chosen name for that global reply variable)
|
|
Oct 4, 2021 at 15:05 | history | edited | Kusalananda♦ | CC BY-SA 4.0 |
added 323 characters in body
|
Oct 4, 2021 at 14:58 | comment | added | Kusalananda♦ | @ilkkachu Thanks. I was definitely not attempting to make generic statements about this. I have amended the text to use more careful language. | |
Oct 4, 2021 at 14:54 | history | edited | Kusalananda♦ | CC BY-SA 4.0 |
added 5 characters in body
|
Oct 4, 2021 at 14:51 | comment | added | ilkkachu |
the exit status of an if statement is zero, if none of the then /else branches were taken. if false; then false; fi leaves the exit status to 0, but if true; then false; fi leaves the exit status to 1. (even though in both cases the last command to run was false , and yes, I think it could return some other nonzero status too.)
|
|
Oct 4, 2021 at 14:46 | history | edited | Kusalananda♦ | CC BY-SA 4.0 |
added 47 characters in body
|
Oct 4, 2021 at 14:40 | history | answered | Kusalananda♦ | CC BY-SA 4.0 |