V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
atom
V2EX  ›  Linux

“$()”是什么含义

  •  
  •   atom · May 7, 2013 · 4440 views
    This topic created in 4742 days ago, the information mentioned may be changed or developed.
    看到过一个快速生成数组的技巧
    array=($(cat file))

    里面的“$()”是什么含义呢?
    5 replies    1970-01-01 08:00:00 +08:00
    bao3
        1
    bao3  
       May 7, 2013   ❤️ 1
    表示将 cat file 执行的结果作为变量
    huangzxx
        2
    huangzxx  
       May 7, 2013   ❤️ 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
       May 7, 2013
    @bao3
    确实是一种新的变量赋值方式。和``一样。
    liwei
        4
    liwei  
       May 7, 2013   ❤️ 1
    man bash

    看 "Command Substitution" 这一节。
    bao3
        5
    bao3  
       May 7, 2013
    @atom no,和 ``有一点点区别的,通常都可以通用。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2483 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 09:53 · PVG 17:53 · LAX 02:53 · JFK 05:53
    ♥ Do have faith in what you're doing.