shell中括号总结: {}, (), (()), [], [[]]
括号总结
() 单小括号
命令组,括号中的命令将会开一个新的子shell执行
括号中变量不能被剩下脚本使用
命令之间分号隔开
命令和括号之间可以没有空格
命令替换,等同于``反引号
$(xxx)会被替换成命令执行结果
初始化数组
arr=(a b c d)
filenum=$(ls | wc -l)
echo ${filenum}
filenum=`ls | wc -l`
echo ${filenum}
arr=(a b c d)
echo ${arr[0]} // a
echo ${arr[@]} //4
(()) 双小括号
整数计算,不支持浮点
((tmp=a+b))
c=$((a+b))
算术运算比较
((c=2+1))
echo $c //3
d=$((1+2))
echo $d //3
// a==1 true
if (($a==1)); then
echo "a==1"
fi
//a==1 true
if ((a==1)); then
echo "a==1"
fi
[] 单中括号
判断命令: 一般参数为比较表达式
会根据比较的结果返回一个退出状态码
可用的比较运算符有:
字符串: ==, !=
整数: -eq, -gt, -lt, -ne, -ge, -le
逻辑: -a(||), -o(&&)
// aa == aa
if [ "aa" == "aa" ]; then
echo "aa == aa"
else
echo "aa != aa"
fi
// 1 < 2
if [ 1 -lt 2 ]; then
echo "1 < 2"
fi
//1 -lt 2 -o aa == bb
if [ 1 -lt 2 -o "aa" == "bb" ]; then
echo "1 -lt 2 -o aa == bb"
fi
[[]] 双中括号
bash程序语言中的关键字,双括号中的支付不会发生参数扩展或者命令替换
支持字符串模式匹配和正则匹配
支持逻辑判断符: &&, ||, <, >
// hello 与 hell? 可以加括号,也可以不加
if [[ hello == hell? ]]; then
echo "hello == hell?"
fi
if [[ 2 -gt 1 && "hello" == "hell?" ]]; then
echo "2 > 1 && hello == hell?"
fi
{} 花括号
使用通配符对花括号内容进行扩展
不允许有空白(除非空白被引用或者转义)
代码块:
不会开一个新的shell, 变量可以被余下脚本使用
每句命令都要有分号
括号两边有空格
`touch {1..4}.txt` // touch 1.txt 2.txt 3.txt 4.txt
`touch {ex1, ex2}.txt` // touch ex1.txt ex2.txt
shell中括号总结: {}, (), (()), [], [[]]的更多相关文章
- Shell中括号的作用
Shell中括号的作用 作者:Danbo 时间:2015-8-7 单小括号() ①.命令组.括号中的命令将会断开一个子Shell顺序执行,所以括号中的变量不能被脚本余下的部分使用.括号中多个命令之间用 ...
- shell中括号的使用
在这里我想说的是几种shell里的小括号,大括号结构和有括号的变量,命令的用法,如下: 1.${var} 2.$(cmd) 3.()和{} 4.${var:-string},${var:+string ...
- shell中括号的特殊用法 linux if多条件判断
一. bash [ ] 单双括号 基本要素: Ø [ ] 两个符号左右都要有空格分隔 Ø 内部操作符与操作变量之间要有空格:如 [ “a” = “b” ] Ø 字符串比较中,&g ...
- (转)shell中括号的特殊用法 linux if多条件判断
一. bash [ ] 单双括号 基本要素: Ø [ ] 两个符号左右都要有空格分隔 Ø 内部操作符与操作变量之间要有空格:如 [ “a” = “b” ] Ø 字符串比较中,&g ...
- Shell编程中括号判断中赋值语句和判断语句
#!/bin/bash declare var="xxx" # without space and use one = #1.judge whether the assignmen ...
- shell之小括号、中括号、大括号
1.Shell中变量的原形:${var} 一串命令的执行 #等价于 $ var=test $ echo $var test #例如,用在这个位置 $ echo ${var}AA testAA 2.命 ...
- shell if条件判断中:双中括号与单中括号的区别
电脑重装了系统,登录虚拟机的shell脚本需重写,在为编写的脚本命名时发现存在同名脚本,才想起来是连接公司服务器的登录脚本,不想写俩脚本,怕记混了,那就整合一下.代码如下: #!/bin/bash#z ...
- Shell 中test 单中括号[] 双中括号[[]] 的区别
Shell test 单中括号[] 双中括号[[]] 的区别 在写Shell脚本的时候,经常在写条件判断语句时不知道该用[] 还是 [[]],首先我们来看他们的类别: $type [ [[ test ...
- shell编程中的单/双 小括号, 中括号, 大括号
linux shell中的变量类型?分字符串或者数字或者bool类型吗? 参考: http://www.cnblogs.com/nufangrensheng/p/3477281.html 不分! sh ...
随机推荐
- z+f profiler 9012
角度分辨率/角度精度 0.0088°/0.02°RMS
- python的try...except
try/except与其他语言相同,在python中,try/except语句主要是用于throw程序正常执行过程中出现的异常,如语法错(python作为脚本语言没有编译的环节,在执行过程中对语法进行 ...
- BZOJ 1677 [Usaco2005 Jan]Sumsets 求和:dp 无限背包 / 递推【2的幂次方之和】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1677 题意: 给定n(n <= 10^6),将n分解为2的幂次方之和,问你有多少种方 ...
- Ajax动态切换按钮
function changeAjax(str, obj) { var idx = $(obj).parent().parent().index(); if(confirm('确定执行操作么?')) ...
- codeforces 609D D. Gadgets for dollars and pounds(二分+贪心)
题目链接: D. Gadgets for dollars and pounds time limit per test 2 seconds memory limit per test 256 mega ...
- 如何理解 Spring 注入
先看一段代码 假设你编写了两个类,一个是人(Person),一个是手机(Mobile). 人有时候需要用手机打电话,需要用到手机的dialUp方法. 传统的写法是这样: Java code publi ...
- BZOJ3932:[CQOI2015]任务查询系统
浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...
- Python3解leetcode Same TreeBinary Tree Level Order Traversal II
问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- UE4 框架
转自:http://www.cnblogs.com/NEOCSL/p/4059841.html 有很多人是从UE3 接触到Unreal,如果你也对UE3非常了解,便能很快的上手UE4.但是,UE4的开 ...
- JS---Math.Random()*10--[0,10)随机变颜色
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...