shell编程之函数

框架:

函数名()

{

  函数封装内容

}

注意:

1.shell函数里面不需要写形参和返回值类型,默认类型是int类型

2.在函数内部,$# 和$*代表函数内部的参数个数和参数内容

 不是命令的参数个数和参数内容

3.再调用的时候参数传递,不需要添加括号

  fun(hello,world);//错误的

  fun hello world//对的

例子

#!/bin/bash

#函数
fun()
{
  echo $*
  echo $#
  for a in $*
  do
      echo $a
  done
  return 5            
}
#调用
fun hello world apple
echo "return value=$?"

 

posted @ 2021-11-06 13:56  从零开始造航母  阅读(55)  评论(0)    收藏  举报