bash function return value

Although Bash has a return statement, the only thing you can specify with it is the function's own exit status (a value between 0 and 255, 0 meaning "success"). So return is not what you want.

You might want to convert your return statement to an echo statement - that way your function output could be captured using $() braces, which seems to be exactly what you want.

Here is an example:

function fun1(){
  echo 34
}

function fun2(){
  local res=$(fun1)
  echo $res
}
Another way to get the return value (if you just want to return an integer 0-255) is $?.

function fun1(){
  return 34
}

function fun2(){
  fun1
  local res=$?
  echo $res
}
Also, note that you can use the return value to use Boolean logic - like fun1 || fun2 will only run fun2 if fun1 returns a non-0 value. The default return value is the exit value of the last statement executed within the function.

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to return value from a function in shell script how to return value from bash function bash call function with return value return function value bash bash script with return value bash function return value to variable sample bash get return value of function shell function return value bash use function return value in if function value return bash return function bash bash return function return function in bash bash return value after done bash return variable from function bash function parameters return value return from function bash bash return a value from a function return bash function return $? from function bash how to return a value in bash bash return funcion function with return value in shell script return something bash function how to return from a function in bash bash function with return using return value of command bash return a value in function in bash return value bash script bash scripting return value from function define function in bash return value return a value from bash script return value from a function bash get return value from method bash return value bash method return value function shell script best way to return values from bash functions bash functions return values bash return from function function bash return shell script function return value bash functionn return value bash func return value echo return value from function bash bash out return value function return bash return value from function shell script bash script function return statement linux bash funkction return value in variable howt to use function return value in bash bash function return to function bash function RETURN bash script return value return from bash function return a value from a function in bash bash if function return value return value from bash function bash return value for function bash functions with return bash return value bash return value from function return value from bash script bash function return value
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source