Source
Pass arguments into a function - Linux Bash Shell Scripting Tutorial Wiki (cyberciti.biz)
Let us see how to pass parameters to a Bash function.
让我们看看如何向 Bash 函数传递参数。
A shell function is nothing but a set of one or more commands/statements that act as a complete routine.
Each function must have a unique name.
Shell functions have their own command line argument or parameters.
Use shell variable $1, $2,..$n to access argument passed to the function.
shell 函数是由一条或多条命令/语句组成的一个完整例程。
每个函数都必须有一个唯一的名称。
shell 函数有自己的命令行参数。
使用 shell 变量 $1、$2…$n 访问传递给函数的参数。
Passing parameters to a Bash function向 Bash 函数传递参数
- The syntax is as follows to create user-defined functions in a shell script:
在 shell 脚本中创建用户定义函数的语法如下:
1 | function_name(){ |
Invoke function
- To invoke the the function use the following syntax:
要调用该函数,请使用以下语法:
1 | my_function_name foo bar |
Where,
my_function_name = Your function name.
foo = Argument # 1 passed to the function (positional parameter # 1).
bar = Argument # 2 passed to the function.
my_function_name = 您的函数名称。
foo = 传递给函数的参数 # 1(位置参数 # 1)。
- bar = 传递给函数的参数 # 2。
Examples
Create a function called fresh.sh:
创建一个名为 fresh.sh 的函数:
1 |
|
Save and close the file. Run it as follows:
保存并关闭文件。按以下步骤运行:
1 | chmod +x fresh.sh |
Sample outputs:
样本输出:
1 | **** calling fresh() 1st time **** |
Let us try one more example. Create a new shell script to determine if given name is file or directory (cmdargs.sh):
让我们再试一个例子。创建一个新的 shell 脚本来判断给定的名称是文件还是目录(cmdargs.sh):
1 |
|
Run it as follows:
运行方法如下
1 | ./cmdargs.sh /etc/resolv.conf |
Sample outputs:
样本输出:
1 | /etc/resolv.conf is a regular file. |
Function shell variables
All function parameters or arguments can be accessed via $1, $2, $3,…, $N.
$0 always point to the shell script name.
$* or $@ holds all parameters or arguments passed to the function.
$# holds the number of positional parameters passed to the function.
所有函数参数或参数都可以通过
$1, $2, $3,..., $N
访问。$0总是指向 shell 脚本名。
$* 或 $@ 保存传递给函数的所有参数。
$# 保存传递给函数的位置参数的个数。
How do I display function name? 如何显示功能名称?
$0 always point to the shell script name. However, you can use an array variable called FUNCNAME which contains the names of all shell functions currently in the execution call stack. The element with index 0 is the name any currently-executing shell function.This variable exists only when a shell function is executing.
$0总是指向 shell 脚本名。不过,您可以使用一个名为 FUNCNAME的数组变量,它包含当前执行调用堆栈中所有 shell 函数的名称。索引为 0 的元素是当前正在执行的 shell 函数的名称。
FUNCNAME in action
Create a shell script called funcback.sh:
创建名为 funcback.sh 的 shell 脚本:
1 |
|
Save and close the file. Run it as follows:
保存并关闭文件。按以下步骤运行:
1 | chmod +x funcback.sh |
Sample outputs:
样本输出:
1 | backup(): directory name not specified |
Return values
返回值
It is possible to pass a value from the function back to the bash using the return command. The return statement terminates the function. The syntax is as follows:
可以使用 return command将函数值传回 bash。return 语句终止函数。语法如下
1 | return |
One can force script to exit with the return value specified by [value]. If [value] is omitted, the return status is that of the last command executed within the function or script.
可以强制脚本以 [value] 指定的返回值退出。如果省略 [value],返回状态将是函数或脚本中最后执行的命令的状态。
Examples
Create a new file called math.sh:
新建一个名为 math.sh 的文件:
1 |
|
Run it as follows:
运行方法如下
1 | chmod +x math.sh |
The above example is straightforward. The return command returns any given value between zero (0) and 255. By default, the value passed by the return statement is the current value of the exit status variable. For example, the following code will return wrong values as it is not between zero (0) and 255.
上面的例子很简单。return 命令 返回介于零 (0) 和 255 之间的任意给定值。默认情况下,return 语句传递的值是 exit status 变量的当前值。例如,以下代码将返回错误的值,因为它不在零(0)和 255 之间。
1 | math 500 100 |
Here is the proper use of return command (bash-task.sh):
下面是 return command的正确用法(bash-task.sh):
1 |
|
Run it as follows:
运行方法如下
1 | chmod +x bash-task.sh |
Passing parameters to a Bash function and return true or false value
向 Bash 函数传递参数并返回 true 或 false 值
How to return custom value from a user-defined function?
As I said earlier, you can’t use return command. However, the workaround is as following using the printf command/echo command:
正如前面所说,不能使用 return command。不过,变通方法如下:使用 printf 命令/echo命令:
1 |
|
Listing functions 列出函数
Use the typeset command or declare command.
使用 typeset command或 declare command。
Show the known functions and their code/definitions显示已知函数及其代码/定义
Open the terminal and then run:
打开终端,然后运行:
1 | declare -f |
How to remove or unset functions 如何删除或取消设置函数
Use the unset command to unset values and attributes of shell variables and functions:
使用 unset 命令 取消设置 shell 变量 和 函数 的值和属性:
1 | unset -f function_name_here |