V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
kchum
V2EX  ›  问与答

怎么在 shell 中用 cat>test1<<EOF 写入文件时不输出 EOF 的变量?

  •  
  •   kchum · Aug 3, 2014 · 7098 views
    This topic created in 4297 days ago, the information mentioned may be changed or developed.
    例如
    cat>test1<<EOF
    $title
    $(git-config --get --bool core.bare)
    $(cd $GIT_DIR; pwd)
    $1
    $GIT_DIR/logs/HEAD
    $(git rev-parse HEAD)
    $$
    $(cd $GIT_DIR; pwd)
    ${GIT_WORK_TREE-..}
    EOF

    需求:test1的内容显示
    $title
    $(git-config --get --bool core.bare)
    $(cd $GIT_DIR; pwd)
    $1
    $GIT_DIR/logs/HEAD
    $(git rev-parse HEAD)
    $$
    $(cd $GIT_DIR; pwd)
    ${GIT_WORK_TREE-..}

    不太熟shell
    放狗搜,没有找到解决方案
    求大神帮看看,非常感谢
    Supplement 1  ·  Aug 3, 2014
    结贴
    感谢2楼提示,我又试了下,\$可以。
    眼困真的要休息先。
    6 replies    2014-08-03 10:47:25 +08:00
    chunyang
        1
    chunyang  
       Aug 3, 2014
    没看懂你想干嘛。

    $ cat > test1 << xxx
    some text
    xxx

    或者不用 xxx,用直接 Ctrl+D 结束
    $ cat > test1
    some text
    ^D
    Cynic222
        2
    Cynic222  
       Aug 3, 2014 via iPhone   ❤️ 1
    我没理解错的话,💲前加反斜线转义?
    hitsmaxft
        3
    hitsmaxft  
       Aug 3, 2014
    有 vim 不用, 这么写, 这是秀操作么...
    kchum
        4
    kchum  
    OP
       Aug 3, 2014
    @hitsmaxft 不是呀,写个小工具,可以把内容放到另外一个URL,wget下来,只是想写在一个文件里。
    kchum
        5
    kchum  
    OP
       Aug 3, 2014
    @chunyang 见4楼
    @Cynic222 诶,昨晚明明试\$时把\也加进去。看来困了真的要休息了,可以啦,谢谢
    gihnius
        6
    gihnius  
       Aug 3, 2014   ❤️ 1
    正确的做法不是使用转义!shell 文档里面有介绍, 加 EOF 引号
    cat > output <<'EOF'
    ...
    EOF

    cat > output <<"EOF"
    ...
    EOF
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2747 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 15:37 · PVG 23:37 · LAX 08:37 · JFK 11:37
    ♥ Do have faith in what you're doing.