Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
webjin
V2EX  ›  Linux

bash 的提问。

  •  
  •   webjin · Jun 16, 2015 · 2546 views
    This topic created in 4009 days ago, the information mentioned may be changed or developed.
    大家好,小弟不才,最近在看一本《linux就是这个范儿》的书籍,里面作者卖了一个关子,想问下这答案是什么,昨天想了一夜,也没想出答案。
    为什么没有“]”这个命令程序呢?
    TerrenceSun
        1
    TerrenceSun  
       Jun 16, 2015
    就像你说的[test_condition],中的"["是一个命令,而不是语法中的括号。如果是括号就一定要成对出现了。
    之所以要有"]",我认为是为了语义上符合逻辑。
    lerosua
        2
    lerosua  
       Jun 16, 2015
    我认为[是个命令,那]可以是参数,形成闭合。
    如果]是命令,那没法搞,因为没有开头是 ] xxx 的语法传统。它的参数按照传统的命令行参数传递顺利,传不进去。
    SoloCompany
        3
    SoloCompany  
       Jun 16, 2015
    2 楼是正确的

    [ a ]

    就是执行命令 [ 带两个参数 a 和 ]
    [ 命令要求最后一个参数必须是 ] 否则报错

    它最终等价于 test a

    [ 和 test 是完全一样的命令,除了对最后一个参数的解释的差异
    webjin
        4
    webjin  
    OP
       Jun 16, 2015
    下面是我man test 得到的解说,但是我英语不才,翻译看不懂专业术语
    =====================================================
    Except for -h and -L, all FILE-related tests dereference symbolic links. Beware that parentheses need to be escaped (e.g., by
    backslashes) for shells. INTEGER may also be -l STRING, which evaluates to the length of STRING.

    NOTE: [ honors the --help and --version options, but test does not. test treats each of those as it treats any other nonempty
    STRING.

    NOTE: your shell may have its own version of test and/or [, which usually supersedes the version described here. Please refer
    to your shell’s documentation for details about the options it supports.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2835 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 04:00 · PVG 12:00 · LAX 21:00 · JFK 00:00
    ♥ Do have faith in what you're doing.