InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Bash shell scripting

  • Bash - add a number to a variable
  • Bash - append text to a variable
  • Bash - how to check if a variable is set
  • Bash - how to compare file timestamps
  • Bash - how to find last command exit status code
  • Bash - how to get main program and current file dir location
  • Bash - how to redirect stderr to stdout or file
  • Bash - how to run custom commands at script exit
  • Bash - how to stop at error
  • Bash - how to use functions - quick tutorial
  • Bash - iterate over array
  • Bash - local and global variables
  • Bash - newline and other escape character in string
  • Bash - pass all arguments from one script to another
  • Bash - set default value if a variable is empty
  • Bash - variables in double quotes vs without quotes
  • Bash associative array tutorial
  • Bash check if file begins with a string
  • Bash shell - check if file or directory exists
  • Can global variables be modified in bash function?
  • Find memcache request hit rate on linux command line
  • How to return a value from bash function
  • Iterate over specific file extension in a dir in shell script
  • Linux - Yesterday's Date in YYYYMMDD format
  • bash - extract urls from xml sitemap
  • bash - how to use regex in if condition
 
  • Home
  • > Tutorials
  • > Bash shell scripting

How to return a value from bash function

By admin on Oct 31, 2015

Returning a variable from functions in bash script can be little tricky. Bash functions are not similar to functions in other languages but these are commands. This article will cover some ways you can return values from bash functions:

Return value using global variable

Global variable can be used to return value from a bash function. Here is sample code to demonstrate it. Save the following code to a file (say script1.sh) and run it.

#!/bin/bash
var1="oldval"
function test1() { 
 var1="newval" ;
}
test1
echo $var1

Here is the outcome from above code:

newval

In case you prefix variable var1 with local then global variable is not modified. Here is the code for it:

#!/bin/bash
var1="oldval"
function test1() { 
  local var1="newval" ;
}
test1
echo $var1

And the outcome for the above code is:

oldval

Return value using echo and subshell

Another approach is to use echo and call function using subshell command $(…). This is better approach whenever it can be used as it does not have to use global variable. Here is sample code for it:

#!/bin/bash
function test1() {
 echo "someval" ;
}
var1=$(test1)
echo $var1

Suggested posts:

  1. Bash – local and global variables
  2. Bash – how to use functions – quick tutorial
  3. Bash – variables in double quotes vs without quotes
  4. Can global variables be modified in bash function?
  5. Bash – how to find last command exit status code
  6. How to capture php code or included file output in a variable
  7. Javascript local and global variables
  8. Bash – how to check if a variable is set
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bash shell scripting, Linux/Unix Command Line, Tutorials

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2023 InfoHeap.

Powered by WordPress