Skip to main content
added 241 characters in body
Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111

if statements deal with exit status of commands. Your function should either return exit status or echo back a string. For your purpose, return seems more suitable. Return 0 for successful completion of function, and anything else if error occured. Example:

$ foo(){ [ -e '/etc/passwd' ] && return 0;  }
$ if foo; then echo "/etc/passwd exists"; fi
/etc/passwd exists

In fact, it should be noted that what you often see as if [ ... ]; then... is exactly the same as if test ...; then... because [ and test are the same command and return zero or non-zero exit status to indicate if error occured.

if statements deal with exit status of commands. Your function should either return exit status or echo back a string. For your purpose, return seems more suitable. Return 0 for successful completion of function, and anything else if error occured. Example:

$ foo(){ [ -e '/etc/passwd' ] && return 0;  }
$ if foo; then echo "/etc/passwd exists"; fi
/etc/passwd exists

if statements deal with exit status of commands. Your function should either return exit status or echo back a string. For your purpose, return seems more suitable. Return 0 for successful completion of function, and anything else if error occured. Example:

$ foo(){ [ -e '/etc/passwd' ] && return 0;  }
$ if foo; then echo "/etc/passwd exists"; fi
/etc/passwd exists

In fact, it should be noted that what you often see as if [ ... ]; then... is exactly the same as if test ...; then... because [ and test are the same command and return zero or non-zero exit status to indicate if error occured.

Source Link
Sergiy Kolodyazhnyy
  • 16.9k
  • 12
  • 58
  • 111

if statements deal with exit status of commands. Your function should either return exit status or echo back a string. For your purpose, return seems more suitable. Return 0 for successful completion of function, and anything else if error occured. Example:

$ foo(){ [ -e '/etc/passwd' ] && return 0;  }
$ if foo; then echo "/etc/passwd exists"; fi
/etc/passwd exists