0

I have the following bash function that returns 0 when tho variable verbos is defined. Have read the bash manual which says that when return command return N, the N is omitted, the return status is that of the last command executed within the function.

How can I use only return at the end, taking the value of N, depending on the return status of [ -n vb ]?

tesverbos  ()
 {
  vb="${verbos+vbset}"  

  if [ -n "$vb" ]; then
    return 0
  else
    return 1
  fi
 }
3
  • test -z "$vb" && return 1. I see no issues with your version either. It's OK to have 100500 return statements in your function if needed. You can also replace return 0 with : (yes, semicolon) or true. Sep 10, 2022 at 14:16
  • would this be equivalent test -z "$vb" ; return ?
    – Vera
    Sep 10, 2022 at 14:45
  • Actually, test -z "$vb" will work. A single statement without any returns, so instead of five line you have just this one. Sep 10, 2022 at 15:18

2 Answers 2

1

How can I use only return at the end, taking the value of N, depending on the return status of [ -n vb ]?

[ -n "$vb" ]
return

Or, if the test is the last command in the function, omit the return entirely.

0

This should work

tesverbos  ()
 {
  vb="${verbos+vbset}"  

  test -n "$vb"
 }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .