Skip to main content
Became Hot Network Question
change the scope of $m
Source Link
fuumind
  • 449
  • 4
  • 13

Consider the following:

echo "hello" > file.txt
is_match1 () {
  local m
  m=$(cat "file.txt" | grep -F "$1")
  if [ -z "$m" ]; then
    return 1
  fi
}
is_match2 () {
  local m
  m=$(cat "file.txt" | grep -F "$1")
  test -z "$m" && return 1
}
is_match1 "hello"
echo "$?"
0
is_match2 "hello"
echo "$?"
1

Why does is_match2 return 1?

Consider the following:

echo "hello" > file.txt
is_match1 () { 
  m=$(cat "file.txt" | grep -F "$1")
  if [ -z "$m" ]; then
    return 1
  fi
}
is_match2 () {
  m=$(cat "file.txt" | grep -F "$1")
  test -z "$m" && return 1
}
is_match1 "hello"
echo "$?"
0
is_match2 "hello"
echo "$?"
1

Why does is_match2 return 1?

Consider the following:

echo "hello" > file.txt
is_match1 () {
  local m
  m=$(cat "file.txt" | grep -F "$1")
  if [ -z "$m" ]; then
    return 1
  fi
}
is_match2 () {
  local m
  m=$(cat "file.txt" | grep -F "$1")
  test -z "$m" && return 1
}
is_match1 "hello"
echo "$?"
0
is_match2 "hello"
echo "$?"
1

Why does is_match2 return 1?

undo
Source Link
fuumind
  • 449
  • 4
  • 13

Consider the following:

echo "hello" > file.txt
is_match1 () { 
  m=$(cat "file.txt" | grep -F "$1")
  if [ -z "$m" ]; then
    return 1
  fi
}
is_match2 () {
  m=$(cat "file.txt" | grep -F "$1")
  test -z "$m" && return 1
}
is_match1 "hello"
echo "$?"
0
is_match2 "hello"
echo "$?"
1

Why does is_match1 and is_match2 return different exit statuses1?

Consider the following:

echo "hello" > file.txt
is_match1 () { 
  m=$(cat "file.txt" | grep -F "$1")
  if [ -z "$m" ]; then
    return 1
  fi
}
is_match2 () {
  m=$(cat "file.txt" | grep -F "$1")
  test -z "$m" && return 1
}
is_match1 "hello"
echo "$?"
0
is_match2 "hello"
echo "$?"
1

Why does is_match1 and is_match2 return different exit statuses?

Consider the following:

echo "hello" > file.txt
is_match1 () { 
  m=$(cat "file.txt" | grep -F "$1")
  if [ -z "$m" ]; then
    return 1
  fi
}
is_match2 () {
  m=$(cat "file.txt" | grep -F "$1")
  test -z "$m" && return 1
}
is_match1 "hello"
echo "$?"
0
is_match2 "hello"
echo "$?"
1

Why does is_match2 return 1?

added 38 characters in body
Source Link
fuumind
  • 449
  • 4
  • 13

Consider the following:

echo "hello" > file.txt
is_match1 () { 
  m=$(cat "file.txt" | grep -F "$1")
  if [ -z "$m" ]; then
    return 1
  fi
}
is_match2 () {
  m=$(cat "file.txt" | grep -F "$1")
  test -z "$m" && return 1
}
is_match1 "hello"
echo "$?"
0
is_match2 "hello"
echo "$?"
1

Why does is_match1 and is_match2 return 1different exit statuses?

Consider the following:

echo "hello" > file.txt
is_match1 () { 
  m=$(cat "file.txt" | grep -F "$1")
  if [ -z "$m" ]; then
    return 1
  fi
}
is_match2 () {
  m=$(cat "file.txt" | grep -F "$1")
  test -z "$m" && return 1
}
is_match1 "hello"
echo "$?"
0
is_match2 "hello"
echo "$?"
1

Why does is_match2 return 1?

Consider the following:

echo "hello" > file.txt
is_match1 () { 
  m=$(cat "file.txt" | grep -F "$1")
  if [ -z "$m" ]; then
    return 1
  fi
}
is_match2 () {
  m=$(cat "file.txt" | grep -F "$1")
  test -z "$m" && return 1
}
is_match1 "hello"
echo "$?"
0
is_match2 "hello"
echo "$?"
1

Why does is_match1 and is_match2 return different exit statuses?

code style for readability
Source Link
Loading
Source Link
fuumind
  • 449
  • 4
  • 13
Loading