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 = 

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

[Bash] Understand and Use Functions in Bash的更多相关文章

  1. -bash: /etc/init.d/nginx: /bin/bash^M: bad interpreter: No such file or directory

    -bash: /etc/init.d/nginx: /bin/bash^M:bad interpreter: No such file or directory 这个使为了弄nginx自启的,然后在官 ...

  2. Bash+R: howto pass parameters from bash script to R(转)

    From original post @ http://analyticsblog.mecglobal.it/analytics-tools/bashr/ In the world of data a ...

  3. Understand JavaScript Callback Functions and Use Them

    In JavaScript, functions are first-class objects; that is, functions are of the type Object and they ...

  4. #VScodd集成Git Bash 命令行 #怎么把Git Bash集成到VScode

    配置 Step1. File-Preferences-Setting Step2. 搜索"terminal>integrated>shell A" Step3. 找到t ...

  5. win-sudo插件解决Git bash 执行脚本报错问题 bash: sudo: command not found

    Windows git bash 默认没有sudo命令,可以添加win-sudo插件实现该功能 curl -s https://raw.githubusercontent.com/imachug/wi ...

  6. Linux下运行bash脚本显示“: /usr/bin/env: "bash\r": 没有那个文件或目录

    用 ./ 运行bash脚本文件出现 报错信息 /usr/bin/env: "bash\r": 没有那个文件或目录 错误原因:这主要是因为bash后面多了\r这个字符的原因.在lin ...

  7. [Bash] View Files and Folders in Bash

    Sometimes when working at the command line, it can be handy to view a file’s contents right in the t ...

  8. CVE: 2014-6271、CVE: 2014-7169 Bash Specially-crafted Environment Variables Code Injection Vulnerability Analysis

    目录 . 漏洞的起因 . 漏洞原理分析 . 漏洞的影响范围 . 漏洞的利用场景 . 漏洞的POC.测试方法 . 漏洞的修复Patch情况 . 如何避免此类漏洞继续出现 1. 漏洞的起因 为了理解这个漏 ...

  9. Linux学习笔记(15)shell基础之Bash基本功能

    1 shell概述 shell是一个命令解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序.用户可以用shell启动.挂起.停止甚至是编写一些程序. shell是一个功能强大 ...

随机推荐

  1. Leetcode 421.数组中两数的最大异或值

    数组中两数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ...

  2. 编绎调试HotSpot JVM及在Eclipse里调试HotSpot一些步骤

    编绎整个OpenJDK要很久,而且有很多东西是不需要的.研究HotSpot的话,其实只要下HotSpot部分的代码就可以了. 下面简单记录下编绎调试HotSpot一些步骤. 一.编绎 进入hotsop ...

  3. javascript学习笔记 - 引用类型 Function

    五 Function类型 每个函数都时Function类型的实例.函数也是对象. 声明函数: function func_name () {} //javascript解析器会在程序执行时率先读取函数 ...

  4. 记账APP市场分析

    文/欧小慧(简书作者)原文链接:http://www.jianshu.com/p/281fcdce3baa著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 一.市场环境 1.理财记账类用 ...

  5. 【bzoj2115】[Wc2011] Xor DFS树+高斯消元求线性基

    题目描述 输入 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边. 图 ...

  6. HDU——1163Eddy's digital Roots(九余数定理+同余定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  7. NOJ——1659求值(log10取对数+floor取整数部分+可有可无的快速幂)

    [1659] 求值 时间限制: 1000 ms 内存限制: 65535 K 问题描述 给你三个数a,b,c,求a的b次的前c位数(不够c位输出全部即可) 输入 输入数据有多组,每组占一行,有三个整数, ...

  8. 刷题总结——Cut the Sequence(POJ 3017 dp+单调队列+set)

    题目: Description Given an integer sequence { an } of length N, you are to cut the sequence into sever ...

  9. [USACO07NOV]牛继电器Cow Relays (最短路,DP)

    题目链接 Solution 非正解 似乎比较蛇啊,先个一个部分分做法,最短路+\(DP\). 在求最短路的堆或者队列中存储元素 \(dis_{i,j}\) 代表 \(i\) 这个节点,走了 \(j\) ...

  10. 关于sass和less做自适应网页的区别

    less 可以这么写  @r: 15rem;   body{margin-top:40/@r}; 但是sass这么写会报错 sass应该这么写 $r: 15; body{margin-top:40re ...