加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

[Bash] Understand and Use Functions in Bash

发布时间:2020-12-15 23:07:54 所属栏目:安全 来源:网络整理
导读:n this lesson,we‘ll go over how bash functions work. Bash functions work like mini bash scripts--you can pass parameters and invoke them just like a bash command. You can also define local variables within a bash function using the? local

n this lesson,we‘ll go over how bash functions work. Bash functions work like mini bash scripts--you can pass parameters and invoke them just like a bash command. You can also define local variables within a bash function using the?local?keyword. Local variables follow similar scope rules present in most programming languages.

?
Define and call a function:
//script.sh
greet() {
  echo "hello world" } greet // call a function
?

?

Pass parameters to the function:

greet() {
  echo "$1 world"
}

greet  "hello"

$1: means the first param passed in to the function.

?

Get the return value:

greet() {
  return "$1 world"
}

greet "Hello"

greeting = $(greet "Hello")

$(): get the return as a result.

?

Global vs local variables:

global = 123

test() {
  echo "global = $global"
  local local_var = "i am a local"
  echo "local_var = $local_var"
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读