Syntax differences between Dash and Bash

Source: Internet
Author: User

Syntax differences between Dash and Bash: In Debian and Ubuntu,/bin/sh already points to dash by default. This is a shell different from bash. It mainly appears to execute scripts, instead of interaction, it is faster, but the function is much less than bash, And the syntax strictly complies with the POSIX standard. The following briefly lists the issues that need to be paid attention to when migrating from bash to dash. define the function bash: In bash, the function is the keyword igi @ gentoo ~ $ Foo () {echo $0;} igi @ gentoo ~ $ Foo/bin/bashigi @ gentoo ~ $ Function foo2 () {echo $0;} igi @ gentoo ~ $ Foo2/bin/bashdash: The function keyword $ foo () {echo $0 ;}$ foodash $ function foo2 () {echo $0;} dash: syntax error: "(" unexpected2.select var in list; do command; done bash: Support for igi @ gentoo ~ $ Select input in a B> do> case $ input in> A)> echo 'input: a'> break >;;> B)> echo 'input: B '> break >;;> esac> done1) A2) B #? 1 Input: Aigi @ gentoo ~ $ Echo $0/bin/bashdash: not supported. Alternative method: use while + read + case to implement menu () {echo-n "1) A; \ n2) B \ n> ";} menuwhile read inputdo case $ input in 1) echo 'A' break; 2) echo 'B' break; *) menu continue ;; esacdone3. echo {0 .. 10} bash: {n .. m} expand igi @ gentoo ~ $ Echo $0/bin/bashigi @ gentoo ~ $ Echo {0 .. 10} 0 1 2 3 4 5 6 7 8 9 10 dash: not supported. Alternative method: Use seq external command $ echo $0 dash $ echo {0 .. 10} {0 .. 10} $ echo 'seq 0 10' 0 1 2 3 4 5 6 7 8 9 104. here string bash: here string igi @ gentoo ~ is supported ~ $ Cat <"string" stringigi @ gentoo ~ $ Echo $0/bin/bashdash: not supported. Alternative method: here documents ents $ echo $0 dash $ cat <"string" dash: Syntax error: redirection unexpected $ cat <EOF> string> EOFstring5.> & word redirection standard output and standard error bash: When word is not a number,> & word: Redirection standard error and standard output to file word. Common usage> &/dev/null igi @ gentoo ~ /Test $ lsaigi @ gentoo ~ /Test $ ls a response: cannot access B: No such file or directoryaigi @ gentoo ~ /Test $ ls a B> & amp;/dev/nulligi @ gentoo ~ /Test $ ls a B>/dev/null 2> & 1igi @ gentoo ~ /Test $ echo $0/bin/bashdash:> & word, word does not support non-numbers. Alternative method:> word 2> & 1; common usage>/dev/null 2> & 1 $ echo $0 dash $ ls aa $ ls a repository: cannot access B: no such file or directorya $ ls a B> &/dev/nulldash: Syntax error: Bad fd number $ ls a B>/dev/null 2> & 1 $6. array bash: supports arrays, and bash4 supports associating arrays igi @ gentoo ~ /Test $ echo $0/bin/bashigi @ gentoo ~ /Test $ array = (a B c) igi @ gentoo ~ /Test $ echo $ {array [2]} cdash: arrays are not supported. Alternative methods, using the variable name + serial number to achieve similar results $ for I in a B c> do> id =$ ($ {id: =-1} + 1)> eval array _ $ id = $ I> done $ echo $ {array_1} B $ echo $ 0dash is a very painful method, so it is not recommended not to use 7. substring extension bash: support $ {parameter: offset: length}, $ {parameter: offset} igi @ gentoo ~ /Test $ string = 'hello' igi @ gentoo ~ /Test $ echo $ {string: 1: 3} elligi @ gentoo ~ /Test $ echo $ {string: 1} elloigi @ gentoo ~ /Test $ echo $0/bin/bashdash: not supported. Alternative method: use expr or cut external commands instead of $ string = 'hello' $ expr substr "$ string" 2 3ell $ echo "$ string" | cut-c2-4ell $ expr substr "$ string" 2 "$ {# string}" ello $ echo "$ string" | cut-c2-ello $ echo $0 dash $8. case-sensitive conversion bash: supports $ {parameter ^ pattern}, $ {parameter ^ pattern}, $ {parameter, pattern}, $ {parameter, pattern} igi @ gentoo ~ /Test $ string = "abcABC" igi @ gentoo ~ /Test $ echo $ {string ^} ABCABCigi @ gentoo ~ /Test $ echo $ {string,} abcabcigi @ gentoo ~ /Test $ echo $ {string ^ B} aBcABCigi @ gentoo ~ /Test $ echo $0/bin/bashdash: not supported. Alternative method: use tr/sed/awk and other external commands to convert $ 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. process replacement <(command),> (command) bash: support process replacement igi @ gentoo ~ $ Diff <(seq 3) <(seq 4) 3a4> 4igi @ gentoo ~ $ Echo $0/bin/bashdash: not supported. Alternative method: transfer $ diff <(seq 3) <(seq 4) dash: Syntax error: "(" unexpected $ seq 3> tmp1 $ seq 4> tmp2 $ diff tmp1 tmp23a4> 4 $ echo $0 dash $10. [string1 = string2] and [string1 = string2] bash: Support for igi @ gentoo ~ $ ['A' = 'a'] & echo 'equal' IGI @ gentoo ~ $ ['A' = 'a'] & echo 'equal' IGI @ gentoo ~ $ Echo $0/bin/bashdash: only $ ['A' = 'a'] & echo 'equal' equal $ ['A' = 'a'] & echo 'equal '[: 2: a: unexpected operator $ echo $0 dash $11. [[enhanced version test bash: supports [[] and supports powerful functions such as regular expression matching. igi @ gentoo ~ $[['Xyz123 '= ~ Xyz [0-9] +] & echo 'equal' IGI @ gentoo ~ $ Echo $0/bin/bashdash: [[] is not supported. The alternative method is to use an external command $[['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: Supports for loop igi @ gentoo ~ in C language format ~ $ For (I = 0; I <= 3; I ++); do echo "$ I"; done igi @ gentoo ~ $ Echo $0/bin/bashdash: for in this format is not supported. The alternative method is while + $ (expression )) implement $ I = 0 $ while ["$ I"-le 3]> do> echo "$ I"> I =$ (I + 1)> done0123 $ echo $0 dash $13. let command and (expression) bash: There is a built-in command let, also supports (expression) mode igi @ gentoo ~ $ I = 0igi @ gentoo ~ $ Let I ++ igi @ gentoo ~ $ Echo $ i1igi @ gentoo ~ $ (I ++) igi @ gentoo ~ $ Echo $ i2igi @ gentoo ~ $ Echo $0/bin/bashdash: not supported. The alternative method is $ (expression )) or use an external command to calculate $ I = 0 $ I = $ (I + 1) $ echo $ i1 $ echo $0 dash $14. $ (expression) bash: supports id ++, id --, ++ id, -- id to the expression igi @ gentoo ~ $ I = 0igi @ gentoo ~ $ Echo $ (I ++) 0igi @ gentoo ~ $ Echo $ i1igi @ gentoo ~ $ Echo $ (++ I) 2igi @ gentoo ~ $ Echo $ i2igi @ gentoo ~ $ Echo $0/bin/bashdash: not supported ++, --, alternative method: 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 $ i1 $ echo $ (I + = 1) 2 $ echo $ i2 $ echo $0 dash $

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.