V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
webjin
V2EX  ›  Linux

bash 的提问。

  •  
  •   webjin · 2015-06-16 09:40:53 +08:00 · 1896 次点击
    这是一个创建于 3250 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大家好,小弟不才,最近在看一本《linux就是这个范儿》的书籍,里面作者卖了一个关子,想问下这答案是什么,昨天想了一夜,也没想出答案。
    为什么没有“]”这个命令程序呢?
    TerrenceSun
        1
    TerrenceSun  
       2015-06-16 10:19:25 +08:00
    就像你说的[test_condition],中的"["是一个命令,而不是语法中的括号。如果是括号就一定要成对出现了。
    之所以要有"]",我认为是为了语义上符合逻辑。
    lerosua
        2
    lerosua  
       2015-06-16 11:21:30 +08:00
    我认为[是个命令,那]可以是参数,形成闭合。
    如果]是命令,那没法搞,因为没有开头是 ] xxx 的语法传统。它的参数按照传统的命令行参数传递顺利,传不进去。
    SoloCompany
        3
    SoloCompany  
       2015-06-16 14:05:23 +08:00
    2 楼是正确的

    [ a ]

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

    它最终等价于 test a

    [ 和 test 是完全一样的命令,除了对最后一个参数的解释的差异
    webjin
        4
    webjin  
    OP
       2015-06-16 15:36:00 +08:00
    下面是我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.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1156 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:39 · PVG 02:39 · LAX 11:39 · JFK 14:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.