Skip to main content
Made it clearer that every function returns "$?" at the end, not just this function.
Source Link

Under the circumstances of your question, m gets the value hello in both functions.

Now look at

test -z "$m" && return 1

What should happen here? The -z test is false, right? So return 1 does not execute. Instead, what does the function return? The Every function returns the value of $? at the end. In this case, that value is 1, the result of the && list.

What you might have wanted to test is

if [ -z "$m" ]; then return 1; fi

compared to

if test -z "$m"; then return 1; fi

The exit status of both these if statements are zero when $m is non-empty, since none of the statements' branches were taken.

From the POSIX standard:

The exit status of the if command shall be the exit status of the then or else compound-list that was executed, or zero, if none was executed.


Note that we may condense both your functions down into

is_match () {
    grep -q -F -e "$1" file.txt
}

Here, we let grep provide the exit status to the caller. We also do not necessarily read the file.txt file to the end, as grep -q quits as soon as it finds a match.

Under the circumstances of your question, m gets the value hello in both functions.

Now look at

test -z "$m" && return 1

What should happen here? The -z test is false, right? So return 1 does not execute. Instead, what does the function return? The function returns the value of $? at the end. In this case, that value is 1, the result of the && list.

What you might have wanted to test is

if [ -z "$m" ]; then return 1; fi

compared to

if test -z "$m"; then return 1; fi

The exit status of both these if statements are zero when $m is non-empty, since none of the statements' branches were taken.

From the POSIX standard:

The exit status of the if command shall be the exit status of the then or else compound-list that was executed, or zero, if none was executed.


Note that we may condense both your functions down into

is_match () {
    grep -q -F -e "$1" file.txt
}

Here, we let grep provide the exit status to the caller. We also do not necessarily read the file.txt file to the end, as grep -q quits as soon as it finds a match.

Under the circumstances of your question, m gets the value hello in both functions.

Now look at

test -z "$m" && return 1

What should happen here? The -z test is false, right? So return 1 does not execute. Instead, what does the function return? Every function returns the value of $? at the end. In this case, that value is 1, the result of the && list.

What you might have wanted to test is

if [ -z "$m" ]; then return 1; fi

compared to

if test -z "$m"; then return 1; fi

The exit status of both these if statements are zero when $m is non-empty, since none of the statements' branches were taken.

From the POSIX standard:

The exit status of the if command shall be the exit status of the then or else compound-list that was executed, or zero, if none was executed.


Note that we may condense both your functions down into

is_match () {
    grep -q -F -e "$1" file.txt
}

Here, we let grep provide the exit status to the caller. We also do not necessarily read the file.txt file to the end, as grep -q quits as soon as it finds a match.

Made it clearer that the exit status of both if-statements is zero when $m is non-zero,
Source Link
Kusalananda
  • 338.9k
  • 37
  • 682
  • 991

Under the circumstances of your question, m gets the value hello in both functions.

Now look at

test -z "$m" && return 1

What should happen here? The -z test is false, right? So return 1 does not execute. Instead, what does the function return? The function returns the value of $? at the end. In this case, that value is 1, the result of the && list.

What you might have wanted to test is

if [ -z "$m" ]; then return 1; fi

compared to

if test -z "$m"; then return 1; fi

The exit status both of both these if statements are zero when $m is non-empty, since none of the statements' branches were taken.

From the POSIX standard:

The exit status of the if command shall be the exit status of the then or else compound-list that was executed, or zero, if none was executed.


Note that we may condense both your functions down into

is_match () {
    grep -q -F -e "$1" file.txt
}

Here, we let grep provide the exit status to the caller. We also do not necessarily read the file.txt file to the end, as grep -q quits as soon as it finds a match.

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).

Under the circumstances of your question, m gets the value hello in both functions.

Now look at

test -z "$m" && return 1

What should happen here? The -z test is false, right? So return 1 does not execute. Instead, what does the function return? The function returns the value of $? at the end. In this case, that value is 1, the result of the && list.

What you might have wanted to test is

if [ -z "$m" ]; then return 1; fi

compared to

if test -z "$m"; then return 1; fi

The exit status both of these if statements are zero when $m is non-empty, since none of the statements' branches were taken.

From the POSIX standard:

The exit status of the if command shall be the exit status of the then or else compound-list that was executed, or zero, if none was executed.


Note that we may condense both your functions down into

is_match () {
    grep -q -F -e "$1" file.txt
}

Here, we let grep provide the exit status to the caller. We also do not necessarily read the file.txt file to the end, as grep -q quits as soon as it finds a match.

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).

Under the circumstances of your question, m gets the value hello in both functions.

Now look at

test -z "$m" && return 1

What should happen here? The -z test is false, right? So return 1 does not execute. Instead, what does the function return? The function returns the value of $? at the end. In this case, that value is 1, the result of the && list.

What you might have wanted to test is

if [ -z "$m" ]; then return 1; fi

compared to

if test -z "$m"; then return 1; fi

The exit status of both these if statements are zero when $m is non-empty, since none of the statements' branches were taken.

From the POSIX standard:

The exit status of the if command shall be the exit status of the then or else compound-list that was executed, or zero, if none was executed.


Note that we may condense both your functions down into

is_match () {
    grep -q -F -e "$1" file.txt
}

Here, we let grep provide the exit status to the caller. We also do not necessarily read the file.txt file to the end, as grep -q quits as soon as it finds a match.

Made it clearer that the exit status of both if-statements is zero when $m is non-zero,
Source Link

Under the circumstances of your question, m gets the value hello in both functions.

Now look at

test -z "$m" && return 1

What should happen here? The -z test is false, right? So return 1 does not execute. Instead, what does the function return? The function returns the value of $? at the end. In this case, that value is 1, the result of the && list.

What you might have wanted to test is

if [ -z "$m" ]; then return 1; fi

compared to

if test -z "$m"; then return 1; fi

The exit status both of these if statements are zero when $m is non-empty, since none of the statements' branches were taken.

From the POSIX standard:

The exit status of the if command shall be the exit status of the then or else compound-list that was executed, or zero, if none was executed.


Note that we may condense both your functions down into

is_match () {
    grep -q -F -e "$1" file.txt
}

Here, we let grep provide the exit status to the caller. We also do not necessarily read the file.txt file to the end, as grep -q quits as soon as it finds a match.

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).

Under the circumstances of your question, m gets the value hello in both functions.

Now look at

test -z "$m" && return 1

What should happen here? The -z test is false, right? So return 1 does not execute. Instead, what does the function return? The function returns the value of $? at the end. In this case, that value is 1, the result of the && list.

What you might have wanted to test is

if [ -z "$m" ]; then return 1; fi

compared to

if test -z "$m"; then return 1; fi

The exit status of these if statements are zero when $m is non-empty, since none of the statements' branches were taken.

From the POSIX standard:

The exit status of the if command shall be the exit status of the then or else compound-list that was executed, or zero, if none was executed.


Note that we may condense both your functions down into

is_match () {
    grep -q -F -e "$1" file.txt
}

Here, we let grep provide the exit status to the caller. We also do not necessarily read the file.txt file to the end, as grep -q quits as soon as it finds a match.

Under the circumstances of your question, m gets the value hello in both functions.

Now look at

test -z "$m" && return 1

What should happen here? The -z test is false, right? So return 1 does not execute. Instead, what does the function return? The function returns the value of $? at the end. In this case, that value is 1, the result of the && list.

What you might have wanted to test is

if [ -z "$m" ]; then return 1; fi

compared to

if test -z "$m"; then return 1; fi

The exit status both of these if statements are zero when $m is non-empty, since none of the statements' branches were taken.

From the POSIX standard:

The exit status of the if command shall be the exit status of the then or else compound-list that was executed, or zero, if none was executed.


Note that we may condense both your functions down into

is_match () {
    grep -q -F -e "$1" file.txt
}

Here, we let grep provide the exit status to the caller. We also do not necessarily read the file.txt file to the end, as grep -q quits as soon as it finds a match.

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).

deleted 168 characters in body
Source Link
Kusalananda
  • 338.9k
  • 37
  • 682
  • 991
Loading
added 154 characters in body
Source Link
Kusalananda
  • 338.9k
  • 37
  • 682
  • 991
Loading
deleted 8 characters in body
Source Link
Kusalananda
  • 338.9k
  • 37
  • 682
  • 991
Loading
added 295 characters in body
Source Link
Kusalananda
  • 338.9k
  • 37
  • 682
  • 991
Loading
added 323 characters in body
Source Link
Kusalananda
  • 338.9k
  • 37
  • 682
  • 991
Loading
added 5 characters in body
Source Link
Kusalananda
  • 338.9k
  • 37
  • 682
  • 991
Loading
added 47 characters in body
Source Link
Kusalananda
  • 338.9k
  • 37
  • 682
  • 991
Loading
Source Link
Kusalananda
  • 338.9k
  • 37
  • 682
  • 991
Loading