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

“$()”是什么含义

  •  
  •   atom · 2013-05-07 13:23:44 +08:00 · 3724 次点击
    这是一个创建于 4000 天前的主题,其中的信息可能已经有所发展或是发生改变。
    看到过一个快速生成数组的技巧
    array=($(cat file))

    里面的“$()”是什么含义呢?
    5 条回复    1970-01-01 08:00:00 +08:00
    bao3
        1
    bao3  
       2013-05-07 13:28:23 +08:00   ❤️ 1
    表示将 cat file 执行的结果作为变量
    huangzxx
        2
    huangzxx  
       2013-05-07 13:31:42 +08:00   ❤️ 1
    # file 的内容
    $ cat file
    a b c

    # 转成数组
    $ array=($(cat file))

    # array 所有元素
    $ echo ${array[@]}
    a b c

    # array 第一个元素
    $ echo ${array[1]}
    a

    # 第二个
    $ echo ${array[2]}
    b

    # 第三个
    $ echo ${array[3]}
    c
    atom
        3
    atom  
    OP
       2013-05-07 14:58:51 +08:00
    @bao3
    确实是一种新的变量赋值方式。和``一样。
    liwei
        4
    liwei  
       2013-05-07 16:34:26 +08:00   ❤️ 1
    man bash

    看 "Command Substitution" 这一节。
    bao3
        5
    bao3  
       2013-05-07 20:50:29 +08:00
    @atom no,和 ``有一点点区别的,通常都可以通用。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   954 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 19:46 · PVG 03:46 · LAX 12:46 · JFK 15:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.