shell中使用函数

函数定义、调用

$ cat te.sh
#!/bin/bash

# define a function
test()
{
  echo "This is a function."
}

# test function call
test
$ sh te.sh

函数库文件

编写函数库文件

#!/bin/bash

# define func
hello()
{
  echo -n "hello $1. "
  credit
}

credit()
{
  echo "Do you need to apply for a credit card ?"
}

载入函数库文件

#!/bin/bash

# call in
. ./lib.sh

hello $1

* 引用函数时不小心带上括号,会报错。

函数返回值

函数返回值,由 return 指定。没有使用时,返回值是函数体最后一条命令的返回值。语法:return [n]

一切代码都是为了生活,一切生活都是调剂
原文地址:https://www.cnblogs.com/argor/p/7908379.html