zoukankan      html  css  js  c++  java
  • linux脚本中单中括号和双中括号的区别

    在 Bash 4.3.11 测试:

    POSIX vs Bash 扩展:

    [ 是 POSIX
    [[ is 一个 Bash 扩展
    常规命令 vs 魔法

    [ 只是一个有着奇怪名字的常规命令。

    ] is just an argument of [ that prevents further arguments from being used.只是一个 [ 的避免更多的参数被使用的参数。

    Ubuntu 16.04 actually has an executable for it at /usr/bin/[ provided by coreutils, but the bash built-in version takes precedence.

    Nothing is altered in the way that Bash parses the command.

    In particular, < is redirection, && and || concatenate multiple commands, ( ) generates subshells unless escaped by \, and word expansion happens as usual.

    [[ X ]] is a single construct that makes X be parsed magically. <, &&, || and () are treated specially, and word splitting rules are different.

    There are also further differences like = and =~.

    In Bashese: [ is a built-in command, and [[ is a keyword: https://askubuntu.com/questions/445749/whats-the-difference-between-shell-builtin-and-shell-keyword

    <

    [[ a < b ]]: lexicographical comparison
    [ a < b ]: Same as above. required or else does redirection like for any other command. Bash extension.
    I could not find a POSIX alternative to this, see: https://stackoverflow.com/questions/21294867/how-to-test-strings-for-less-than-or-equal
    && and ||

    [[ a = a && b = b ]]: true, logical and
    [ a = a && b = b ]: syntax error, && parsed as an AND command separator cmd1 && cmd2
    [ a = a -a b = b ]: equivalent, but deprecated by POSIX
    [ a = a ] && [ b = b ]: POSIX recommendation
    (

    [[ (a = a || a = b) && a = b ]]: false
    [ ( a = a ) ]: syntax error, () is interpreted as a subshell
    [ ( a = a -o a = b ) -a a = b ]: equivalent, but () is deprecated by POSIX
    ([ a = a ] || [ a = b ]) && [ a = b ] POSIX recommendation
    word splitting

    x='a b'; [[ $x = 'a b' ]]: true, quotes not needed
    x='a b'; [ $x = 'a b' ]: syntax error, expands to [ a b = 'a b' ]
    x='a b'; [ "$x" = 'a b' ]: equivalent
    =

    [[ ab = a? ]]: true, because it does pattern matching (* ? [ are magic). Does not glob expand to files in current directory.
    [ ab = a? ]: a? glob expands. So may be true or false depending on the files in the current directory.
    [ ab = a? ]: false, not glob expansion
    = and == are the same in both [ and [[, but == is a Bash extension.
    printf 'ab' | grep -Eq 'a.': POSIX ERE equivalent
    [[ ab =~ 'ab?' ]]: false, loses magic with ''
    [[ ab? =~ 'ab?' ]]: true
    =~

    [[ ab =~ ab? ]]: true, POSIX extended regular expression match, ? does not glob expand
    [ a =~ a ]: syntax error
    printf 'ab' | grep -Eq 'ab?': POSIX equivalent
    Recommendation

    I prefer to always use [].

    There are POSIX equivalents for every [[ ]] construct I've seen.

    If you use [[ ]] you:

    lose portability
    force the reader to learn the intricacies of another bash extension. [ is just a regular command with a weird name, no special semantics are involved.

    没翻译完,待续

  • 相关阅读:
    sqlite
    c++primer
    c++ std find_last_of
    c语言
    boost serialization
    ssh autologin
    c/c++文件相关
    AndroidTreeView等例子
    and
    解决Gradle 依赖下载慢以及android开发释放c盘空间及android虚拟机访问网络--以及访问本机
  • 原文地址:https://www.cnblogs.com/chenminklutz/p/10097180.html
Copyright © 2011-2022 走看看