shell 与 空格
shell 空格
2023-09-27 14:21:53 时间
shell脚本中,不能随意添加空格,否则出错:
1,=等号两边必须无空格。否则出错。如i =$1和i= $1都是错的。但是在()内部不限制如for ((i= 1;i < 3;i= i+1))是正确的。
2,(())内外部括号之间无空格,( () )这样报错。但内部括号内不限制,随便如s=$(( $i+1 ))可以
3,[]是条件判断式,所以前后内部都必须空格。其内部元素之间必须有空格才能识别,否则一律报错,如“while [ $i -lt 6 ]”少一个空格都报错。总之,[]所在行每个独立元素前后都必须有空格(不是一起的就必须空格)。-gt,-lt,-ge,-le,-ne
另外,单引号是字符串限定符,将内部内容作为字符串。而双引号和{}是变量限定符,内部作为变量处理
例子:
#!/bin/bash for ((i= 1;i < 3;i= i+1)) do echo $i; echo '$i'; echo "$i"; done while [ $i -lt 6 ] do i=$(( $i+1 )) echo $i done s=$(( $i+1 )) echo $s
再如: bin=`dirname "$0"` 如果写成bin =`dirname "$0"`就报错
#!/usr/bin/bash source /etc/profile bin=`dirname "$0"` echo $bin echo "$bin" echo '$bin' echo ${bin} echo "${bin}" #inputfile="/home/deve_test_user/liu/observer_report_20130915155111" inputfile="$bin/observer_report_20130915155111" echo $inputfile echo ${inputfile} echo "$inputfile" echo "${inputfile}" 输出: . . $bin . . ./observer_report_20130915155111 ./observer_report_20130915155111 ./observer_report_20130915155111 ./observer_report_20130915155111
从例子中可以看出:
1.{}和“”对于变量没有影响。有没有一样
2.单引号''是变量成为字符串,echo '$bin'输出为$bin
相关文章
- linux shell搜索某个字符串,然后在后面加上字符串?字符串后面插入字符串?sed字符串后面插入字符串?
- Shell读取配置文件、修改配置文件(ini、conf)
- hbase shell基本命令
- 《学习bash》笔记--调试shell程序
- MongoDB(二)mongo shell
- Shell 函数带中横线问题排查
- shell 菱形图案
- 跟老男孩学Linux运维:Shell编程实战1.4 学完本书后可以达到何种Shell编程高度
- python 执行shell 命令,自动化添加***IP地址到iptables
- nodejs zip 批量压缩文件夹 按需过滤文件 批量迁徙mv备份 shell bash 命令
- Shell中去除字符串前后空格的方法
- 也可以使用如下命令更改您的默认 Shell
- SHELL脚本--tr命令用法和特性全解
- shell变量及相关命令
- shell位置参数变量
- 三、shell脚本编程
- shell备份文件时加上时间戳
- Shell IF,linux if,linux四则运算
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---54
- linux的shell脚本