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. Maven项目下Tomcat插件选择方法

    1. 进入Tomcat官网:http://tomcat.apache.org/ 选择Maven plugin 2. 选择版本 3. 查看版本对应的插件版本: 有两种方式添加:如下图所示:

  2. python正则re模块

    今日内容: 知识点一:正则 什么是正则:  就是用一系列具有特殊含义的字符组成一套规则,改规则用来描述具有某一特征的字符串  正则就是用来在一个大的字符串中取出符合规则的小字符串   为什么用正则:  ...

  3. 【bzoj2430】[Poi2003]Chocolate 贪心

    题目描述 有一块n*m的矩形巧克力,准备将它切成n*m块.巧克力上共有n-1条横线和m-1条竖线,你每次可以沿着其中的一条横线或竖线将巧克力切开,无论切割的长短,沿着每条横线切一次的代价依次为y1,y ...

  4. Multiset ------ 多重集合

    Multiset的中文名是多重集合,其实就是集合的扩展版.唯一的不同是集合中一个值只能出现一次,而多重集合中一个值可以出现多次. 粗略看了看MSDN,在STL中,multiset和set的成员函数声明 ...

  5. 前端CSS规范大全(转)

    一.文件规范 1.文件均归档至约定的目录中. 具体要求通过豆瓣的CSS规范进行讲解: 所有的CSS分为两大类:通用类和业务类.通用的CSS文件,放在如下目录中: 基本样式库 /css/core 通用U ...

  6. NOIP2017赛前模拟1:总结

    题目: 1.造盒子 题目描述 企鹅豆豆收到了面积为 K 的一块橡皮泥.但是他没有合适的盒子来装下这个橡皮泥.所以他打算造一个盒子. 制造台是有方形网格的平台,每个小正方形边长为 1 .现在豆豆有两类木 ...

  7. 给某个li标签家样式

    HTML: <div class="tabs clearfix"> <ul id="der"> <li ><a hre ...

  8. 欧拉回路 & 欧拉路径

    欧拉路径 & 欧拉回路 概念 欧拉路径: 如果图 G 种的一条路径包括所有的边,且仅通过一次的路径. 欧拉回路: 能回到起点的欧拉路径. 混合图: 既有无向边又有无向边的图. 判定 无向图 一 ...

  9. 数据库操作之—— explain 的type解释

    (1)SYSTEM (2)CONST (3)EQ_REF (4)REF (5)REF_OR_NULL (6)RANGE (7)INDEX_SCAN (8)ALL (9)UNIQUE_SUBQUERY ...

  10. python -- DNS处理模块dnspython

    简介 dnspython – 是python实现的一个DNS工具包,利用其查询功能来实现dns的服务监控及解析结果的校验 安装dnspython pip install dnspython 使用 常见 ...