Home > Software design >  How i can use return a value after then in if-else in bash
How i can use return a value after then in if-else in bash

Time:08-11

code

if [[ $var ]]; then
     return $var
else 
     exit 0
fi   

OR

if [[ $var ]];then 
     $var
fi
      

OR

in Bash language. the error i am getting is

$ bash script.sh
  bash script.sh: line 1: value: command not found

CodePudding user response:

Returning values should be in echoes

#!/bin/bash
var=$1
if [[ $var ]]; then
     echo $var
else 
     exit 0
fi   

execution

$ echo $(./if-return.sh hello)

result

hello

CodePudding user response:

The question is ambiguous, as I see confirmed in the varying answers. I'll answer for both interpretations I'd have. if-then-else syntax is not needed in either case.

  1. You want your function to return with an exit code of $var if it's set, else return with exit 0.

    return ${var:-0}

  2. You want your function to output the value of $var if it's set, else output 0.

    echo ${var:-0}

If neither of those are what you're looking for, I do not understand your question - please add more detail/explanation.

  • Related