bash与dash的差别

2024-02-02 01:58
文章标签 bash 差别 dash

本文主要是介绍bash与dash的差别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

如今Debian和Ubuntu中,/bin/sh默认已经指向dash,这是一个不同于bash的shell,它主要是为了执行脚本而出现,而不是交互,
它速度更快,但功能相比bash要少很多,语法严格遵守POSIX标准,下面简要列举下从bash迁移到dash一般需要注意的问题1.定义函数
bash: function在bash中为关键字
igi@gentoo ~ $ foo(){ echo $0;}
igi@gentoo ~ $ foo
/bin/bash
igi@gentoo ~ $ function foo2(){ echo $0;}
igi@gentoo ~ $ foo2
/bin/bash
dash: dash中没有function这个关键字
$ foo(){ echo $0;}
$ foo
dash
$ function foo2(){ echo $0;}
dash: Syntax error: "(" unexpected
2.select var in list; do command; done
bash:支持
igi@gentoo ~ $ select input in A B
> do
>   case $input in
>     A)
>        echo 'Input:A'
>        break
>        ;;
>     B)
>        echo 'Input:B'
>        break
>        ;;
>   esac
> done
1) A
2) B
#? 1
Input:A
igi@gentoo ~ $ echo $0
/bin/bash
dash:不支持, 替代方法:采用while+read+case来实现
menu(){ echo -n "1)A;/n2)B/n>";}
menu
while read input
docase $input in1)echo 'A'break;;2)echo 'B'break;;*)menucontinue;;esac
done
3. echo {0..10}
bash:支持{n..m}展开
igi@gentoo ~ $ echo $0
/bin/bash
igi@gentoo ~ $ echo {0..10}
0 1 2 3 4 5 6 7 8 9 10
dash:不支持,替代方法, 采用seq外部命令
$ echo $0
dash
$ echo {0..10}
{0..10}
$ echo `seq 0 10`
0 1 2 3 4 5 6 7 8 9 10
4. here string
bash:支持here string
igi@gentoo ~ $ cat <<<"string"
string
igi@gentoo ~ $ echo $0
/bin/bash
dash:不支持, 替代方法:可采用here documents
$ echo $0
dash
$ cat <<<"string"
dash: Syntax error: redirection unexpected
$ cat <<EOF
> string
> EOF
string
5. >&word重定向标准输出和标准错误
bash: 当word为非数字时,>&word变成重定向标准错误和标准输出到文件word, 常见用法>&/dev/null
igi@gentoo ~/test $ ls
a
igi@gentoo ~/test $ ls a b
ls: cannot access b: No such file or directory
a
igi@gentoo ~/test $ ls a b >&/dev/null
igi@gentoo ~/test $ ls a b >/dev/null 2>&1
igi@gentoo ~/test $ echo $0
/bin/bash
dash: >&word, word不支持非数字, 替代方法: >word 2>&1; 常见用法 >/dev/null 2>&1
$ echo $0
dash
$ ls a
a
$ ls a b
ls: cannot access b: No such file or directory
a
$ ls a b >&/dev/null
dash: Syntax error: Bad fd number
$ ls a b >/dev/null 2>&1
$
6. 数组
bash: 支持数组, bash4支持关联数组
igi@gentoo ~/test $ echo $0
/bin/bash
igi@gentoo ~/test $ array=( a b c )
igi@gentoo ~/test $ echo ${array[2]}
c
dash: 不支持数组,替代方法, 采用变量名+序号来实现类似的效果
$ for i in a b c
> do
> id=$((${id:=-1}+1))
> eval array_$id=$i
> done
$ echo ${array_1}
b
$ echo $0
dash
很蛋疼的方法,非不得以不建议这么用
7. 子字符串扩展
bash: 支持${parameter:offset:length},${parameter:offset}
igi@gentoo ~/test $ string='hello'
igi@gentoo ~/test $ echo ${string:1:3}
ell
igi@gentoo ~/test $ echo ${string:1}
ello
igi@gentoo ~/test $ echo $0
/bin/bash
dash: 不支持, 替代方法:采用expr或cut外部命令代替
$ string='hello'
$ expr substr "$string" 2 3
ell
$ echo "$string" | cut -c2-4
ell
$ expr substr "$string" 2 "${#string}"
ello
$ echo "$string" | cut -c2-
ello
$ echo $0
dash
$
8. 大小写转换
bash: 支持${parameter^pattern},${parameter^^pattern},${parameter,pattern},${parameter,,pattern}
igi@gentoo ~/test $ string="abcABC"
igi@gentoo ~/test $ echo ${string^^}
ABCABC
igi@gentoo ~/test $ echo ${string,,}
abcabc
igi@gentoo ~/test $ echo ${string^^b}
aBcABC
igi@gentoo ~/test $ echo $0
/bin/bash
dash: 不支持,替代方法:采用tr/sed/awk等外部命令转换
$ string='abcABC'
$ echo "$string" | tr '[a-z]' '[A-Z]'
ABCABC
$ echo "$string" | tr '[A-Z]' '[a-z]'
abcabc
$ echo "$string" | sed 's/b//U&/g'
aBcABC
$
9. 进程替换<(command), >(command)
bash: 支持进程替换
igi@gentoo ~ $ diff <(seq 3) <(seq 4)
3a4
> 4
igi@gentoo ~ $ echo $0
/bin/bash
dash: 不支持, 替代方法, 通过临时文件中转
$ diff <(seq 3) <(seq 4)
dash: Syntax error: "(" unexpected
$ seq 3 >tmp1
$ seq 4 >tmp2
$ diff tmp1 tmp2
3a4
> 4
$ echo $0
dash
$
10. [ string1 = string2 ] 和 [ string1 == string2 ]
bash: 支持两者
igi@gentoo ~ $ [ 'a' = 'a' ] && echo 'equal'
equal
igi@gentoo ~ $ [ 'a' == 'a' ] && echo 'equal'
equal
igi@gentoo ~ $ echo $0
/bin/bash
dash: 只支持=
$ [ 'a' = 'a' ] && echo 'equal'
equal
$ [ 'a' == 'a' ] && echo 'equal'
[: 2: a: unexpected operator
$ echo $0
dash
$
11. [[ 加强版test
bash: 支持[[ ]], 可实现正则匹配等强大功能
igi@gentoo ~ $ [[ 'xyz123' =~ xyz[0-9]+ ]] && echo 'equal'
equal
igi@gentoo ~ $ echo $0
/bin/bash
dash: 不支持[[ ]], 替代方法,采用外部命令
$ [[ 'xyz123' =~ xyz[0-9]+ ]] && echo 'equal'
dash: [[: not found
$ echo 'xyz123' | grep -q 'xyz[0-9]/+' && echo 'equal'
equal
$ echo $0
dash
$
12. for (( expr1 ; expr2 ; expr3 )) ; do list ; done
bash: 支持C语言格式的for循环
igi@gentoo ~ $ for((i=0;i<=3;i++));do echo "$i";done
0
1
2
3
igi@gentoo ~ $ echo $0
/bin/bash
dash: 不支持该格式的for, 替代方法,用while+$((expression))实现
$ i=0
$ while [ "$i" -le 3 ]
> do
> echo "$i"
> i=$((i+1))
> done
0
1
2
3
$ echo $0
dash
$
13. let命令和((expression))
bash: 有内置命令let, 也支持((expression))方式
igi@gentoo ~ $ i=0
igi@gentoo ~ $ let i++
igi@gentoo ~ $ echo $i
1
igi@gentoo ~ $ ((i++))
igi@gentoo ~ $ echo $i
2
igi@gentoo ~ $ echo $0
/bin/bash
dash: 不支持,替代方法,采用$((expression))或者外部命令做计算
$ i=0
$ i=$((i+1))
$ echo $i
1
$ echo $0
dash
$
14. $((expression))
bash: 支持id++,id--,++id,--id这样到表达式
igi@gentoo ~ $ i=0
igi@gentoo ~ $ echo $((i++))
0
igi@gentoo ~ $ echo $i
1
igi@gentoo ~ $ echo $((++i))
2
igi@gentoo ~ $ echo $i
2
igi@gentoo ~ $ echo $0
/bin/bash
dash: 不支持++,--, 替代方法:id+=1,id-=1, id=id+1,id=id-1
$ i=0
$ echo $((i++))
dash: arithmetic expression: expecting primary: "i++"
$ echo $i;i=$((i+1))
0
$ echo $i
1
$ echo $((i+=1))
2
$ echo $i
2
$ echo $0
dash
$

本文转自:http://blog.csdn.net/dianhuiren/article/details/6440209

这篇关于bash与dash的差别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/669149

相关文章

如何使用 Bash 脚本中的time命令来统计命令执行时间(中英双语)

《如何使用Bash脚本中的time命令来统计命令执行时间(中英双语)》本文介绍了如何在Bash脚本中使用`time`命令来测量命令执行时间,包括`real`、`user`和`sys`三个时间指标,... 使用 Bash 脚本中的 time 命令来统计命令执行时间在日常的开发和运维过程中,性能监控和优化是不

bat脚本启动git bash窗口,并执行命令方式

《bat脚本启动gitbash窗口,并执行命令方式》本文介绍了如何在Windows服务器上使用cmd启动jar包时出现乱码的问题,并提供了解决方法——使用GitBash窗口启动并设置编码,通过编写s... 目录一、简介二、使用说明2.1 start.BAT脚本2.2 参数说明2.3 效果总结一、简介某些情

bash: arm-linux-gcc: No such file or directory

ubuntu出故障重装了系统,一直用着的gcc使用不了,提示bash: arm-linux-gcc: No such file or directorywhich找到的命令所在的目录 在google上翻了一阵发现此类问题的帖子不多,后来在Freescale的的LTIB环境配置文档中发现有这么一段:     # Packages required for 64-bit Ubuntu

file-max与ulimit的关系与差别

http://zhangxugg-163-com.iteye.com/blog/1108402 http://ilikedo.iteye.com/blog/1554822

小工具:输出Houdini里节点间参数的差别

需求 节点的参数影响了节点的行为。因此对于节点使用者来说,比较节点间参数的差异就可以明白其行为的差异了。 然而有些节点的参数数量实在太多,比较其参数间的差异会比较麻烦,在没有工具的情况下只能来回在节点间跳转才能比较出参数的不同。 我想,其实可以使用Houdini的Python模块写代码来自动比较并输出不同,这并不麻烦。最后如果能输出更易于阅读的格式(比如csv表格,或Markdown表格语法

bash脚本2_对比多个不同版本同名文件的异同

bash脚本2_对比多个不同版本同名文件的异同 #!/bin/bashFOLDER_A="$1"FOLDER_B="$2"IGNORE_STRING="loc_timestamp"subfolders=$(ls -d "$FOLDER_A"/*/)for subfolderA in $subfolders; dosubfolder_name=$(basename "$subfol

软件测试学习笔记丨Linux-Bash编程语法

本文转自测试人社区,原文链接:https://ceshiren.com/t/topic/32091 一、Bash编程基础 1.1 变量 1.1.1 语法 Variable_name=value 1.1.2 变量定义的规则 变量名区分大小写,a和A为两个不同的变量;变量名可以使用大小写字母混编的形式进行编写;变量名与值之间的=两侧都不能有空格;在读取或打印变量时,需使用$+变量名;

bash脚本1_完成文件夹的更新

bash脚本1_完成文件夹的更新 功能: 文件夹A,文件夹B 文件夹A是文件夹B的子集。 两者的层级一致,内部都包含多个子文件夹 写一个bash脚本将文件夹B中出现在文件A中的子文件夹替换为文件夹A的子文件夹 #!/bin/bash# 确保脚本以两个参数运行if [ "$#" -ne 2 ]; thenecho "Usage: $0 folderA folderB"exit 1

amfphp1.9和amfphp2.2,1差别

amfphp1.9和amfphp2.2,1差别  1,services-config.xml 该配置文件的不同 ,主要是<endpoint uri的差别                        1.9:<?xml version="1.0" encoding="UTF-8"?> <services-config>    <services>         <service id='

NewStringUTF和GetStringUTFChars 的差别

NewStringUTF 和 GetStringUTFChars 是 JNI(Java Native Interface)提供的两个函数,它们都用于在 JNI 代码中处理字符串,但用途和行为有所不同: NewStringUTF 功能:NewStringUTF 用于在 JNI 代码中创建一个新的 Java 字符串对象。它接受一个 UTF-8 编码的 C 字符串作为输入,并在 Java 虚拟机中创建