Returning Values from Bash Functions

  Bash functions, unlike functions in most programming languages do not allow you to return a value to

the caller. When a bash function ends its return value is its status: zero for success, non-zero for failure.To

return values, you can set a global variable with the result, or use command substitution, or you can pass in

the name of a variable to use as the result variable. The examples below describe these different mechanisms.

  Although bash has a return statement, the only thing you can specify with it is the function‘s status, which

is a numeric value like the value specified in an exit statement. The status value is stored in the $? variable. If

a function does not contain a return statement, its status is set based on the status of the last statement executed

in the function. To actually return arbitrary values to the caller you must use other mechanisms.

  The simplest way to return a value from a bash function is to just set a global variable to the result. Since

all variables in bash are global by default this is easy:

function myfunc()
{
  myresult=‘some value‘
}

myfunc
echo $myresult

  The code above sets the global variable myresult to the function result. Reasonably simple, but as we all

know, using global variables, particularly in large programs, can lead to difficult to find bugs.

  A better approach is to use local variables in your functions. The problem then becomes how do you get

the result to the caller. One mechanism is to use command substitution:

function myfunc()
{
  local myresult=‘some value‘
  echo "$myresult"
}

result=$(myfunc)echo $result

  Here the result is output to the stdout and the caller uses command substitution to capture the value

in a variable. The variable can then be used as needed.

摘录自:

Returning Values from Bash Functions: http://www.linuxjournal.com/content/return-values-bash-functions

时间: 2024-08-07 08:06:22

Returning Values from Bash Functions的相关文章

Zend API:深入 PHP 内核

Introduction Those who know don't talk. Those who talk don't know. Sometimes, PHP "as is" simply isn't enough. Although these cases are rare for the average user, professional applications will soon lead PHP to the edge of its capabilities, in t

Examples for Creating Oracle Functions

Examples for Creating Oracle Functions Mika Wendelius, 1 Apr 2012 CPOL    4.46 (11 votes)   Rate: vote 1vote 2vote 3vote 4vote 5   The article starts with building a simple toolkit package using basic PL/SQL but also covers some more advanced techniq

Introduction to Writing Functions in R

在R中编写函数 为啥要用函数 为了代码更加可读,减少重复代码==lazy You can type less code, saving effort and making your analyses more readable. You can reuse your code from project to project. in R function are a type of variable ,so you assign functions using left arrow,just li

Test of returning array efficiency from Fortran subprograms

Fortran has two kinds of subprograms: subroutine and function. Usually, subroutine is a combination of several procedures generating side effects without returning values, while the purpose of function is to return values after some operations. In fa

select、bash函数初识及rpm命令详解

8月18号,主要学习内容: 一.循环的特殊用法及select 二.bash函数基础 三.rpm命令 一.循环的特殊用法及select 1)while循环的特殊用法(遍历文件的每一行):  while read line; do 循环体 done < /PATH/FROM/SOMEFILE 依次读取/PATH/FROM/SOMEFILE文件中的每一行,且将行赋值给变量line 2)((-))格式 可以用于算术运算 双小括号方法也可以使bash Shell实现C语言风格的变量操作 #I=10 #((

Functional PHP 5.3 Part I - What are Anonymous Functions and Closures?

One of the most exciting features of PHP 5.3 is the first-class support for anonymous functions. You may have heard them referred to as closures or lambdas as well. There's a lot of meaning behind these terms so let's straighten it all out. What is t

Bash String Manipulation Examples – Length, Substring, Find and Replace--reference

In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable with its value. This feature of shell is called parameter expansion. But parameter expansion has numerous other forms which allow you to expand a parame

Following a Select Statement Through Postgres Internals

This is the third of a series of posts based on a presentation I did at the Barcelona Ruby Conference called “20,000 Leagues Under ActiveRecord.” (posts: one two and video). Preparing for this presentation over the Summer, I decided to read through p

linux c coding style

Linux kernel coding style This is a short document describing the preferred coding style for the linux kernel. Coding style is very personal, and I won't _force_ my views on anybody, but this is what goes for anything that I have to be able to mainta