Ubuntudash Shell
Https://wiki.ubuntu.com/DashAsBinSh
Http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_title.html
From Ubuntu 6.10, dash (theDebian almshells) is used by default instead of bash (the GNUBourne-Again Shell ). but Login Shell is still bash. the reason is that dash is faster and more efficient, and it complies with POSIX specifications. Ubuntu runs a lot of shell scripts during startup. Using dash can speed up startup.
- If you solve the problem caused by bash and dash compatibility
- Write "#! /Bin/bash"
- In Makefile, you can set
SHELL =/bin/bash
- To change the default value to bash, run the following command and select no. Note that this will affect all system scripts. If a script requires a specific dash function, it will cause a problem (this case is rare ).
Sudodpkg-reconfigure dash
- Avoid using the bash extension feature (bashism) in newly written shell scripts ).
- Use the checkbashisms command of the devscripts package to check whether bashism exists in the shell script.
- Run the info autoconf command to install the autoconf-doc package. Read the documentation in "Portable Shell" to learn more about POSIX Shell.
- Avoid using-a,-o in the "[" command (test). Use multiple "[]" commands and connect with "&", "|.
For example, the following shell statement
[\ ("$ Foo" = "$ bar"-a-f/bin/baz \)-o! -X/bin/quux]
Should be replaced:
(["$ Foo" = "$ bar"] & [-f/bin/baz]) | [! -X/bin/quux])
- Instead of using the "[[" command, you should use the "[" command
- Use $ ((...)) Instead ((...)) Perform arithmetic calculations.
- You cannot use $ (n ++), $ (-- n), instead of $ (n = n + 1 )) and $ (n = N-1 ))
- Do not use {} For character extension, such as/usr/lib/libfoo. {a, so };
- Avoid using $ '... 'Extended escape characters. For example, replace $ '\ t' with "$ (printf' \ t'
- Do not use $ "... "For string translation. Use the gettext. sh script.
- Most of the $ {...} Variable extension is portable. But the following are not.
- $ {!...} Run the eval command for non-direct variable extension.
- $ {Parameter/pattern/string} to replace the pattern
- $ {Parameter: offset: length} intercepts a substring
- Do not use $ {parm /? /Pat [/str]} should replace characters, and echo, sed, grep, awk and other commands should be used. For example:
OPENGL_VERSION = $ (glxinfo | grep "OpenGL version string :")
OPENGL_VERSION =$ {OPENGL_VERSION /*:/}
Use:
OPENGL_VERSION = $ (glxinfo | grep "OpenGL version string:" | awk 'in in {FS = ": [[: space:] +" };{ print $2 }')
- Do not use $ {foo: 3 [: 1]} to cut substrings. Use echo, sed, grep, awk, and other commands.
For example:
String_after = "somestring"
String =$ {string_after: 0: 3}
Use:
String = $ (echo $ string_after | awk '{string = substr ($, 3); print string ;}')
- Use [!] In case statements Instead of [^]. For example:
Case "foo" in
[^ F] *)
Echo 'not f *'
;;
Esac
Replace:
Case "foo" in
[! F] *)
Echo 'not f *'
;;
Esac
- Dash does not support $ LINENO, although it is a POSIX standard.
- Do not use $ PIPESTATUS
- Avoid using $ RANDOM, but read/dev/urandom or/dev/random. For example:
Random = 'hexdump-n 2-E'/2 "% u" '/dev/urandom'
- Some echo options are not portable, and may not be supported by other shells. For example,-e,-n
- Do not add the function keyword before the function name.
- Do not use the let Command. Use = to assign values directly. For example
Let time = 10 is the same as time = 10
Let time -- the same as time =$ (time-1)
- Bash and dash have different interpretations of the local keyword.
Local a = 5 B = 6; // dash: a and B are global variables, While bash considers a and B as local variables.
- Printf % q | % B is not supported
- Do not use the select keyword, which is only supported by bash.
- The source command is also supported by bash. The '.' command should be used.
- During path search, 'dash 'does not support '~ 'Extended, use $ HOME
- Declare or typeset is not supported
- Bash and dash have different options for ulimit and type.
- Time is a built-in bash command, but the time program must be used in dash.
- Kill-[0-9] or-[A-Z] is the bash built-in command
- In bash, if read is not connected to a variable, it is saved in the REPLY variable. In dash, read REPLY should be used instead.
- Do not use <, but use <instead. For example:
$ Cat <"$ HOME is where the heart is ."
/Home/ralphis where the heart is.
Replace:
$ Cat <E
> $ HOME is where the heart is.
> E
/Home/ralphis where the heart is.
$