从终端创建多行文件时如何缩进?

问题描述 投票:0回答:2

所以我需要一个命令来使输出看起来像这样:

    [core]
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
    [remote "origin"]
            url = ssh://[email protected]/user/address.git
            fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
            remote = origin
            merge = refs/heads/master

我试过以下,

printf "[core]\n    repositoryformatversion = 0\n   filemode = true\n   bare = false\n  logallrefupdates = true\n   logallrefupdates = true\n[remote "origin"]\n    url = ssh://[email protected]/user/address.git\n   fetch = +refs/heads/*:refs/remotes/origin/*\n[branch "master"]\n    remote = origin\n   merge = refs/heads/master" > config

但它会创建一个包含这些内容的文件

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
logallrefupdates = true
[remote origin]
url = ssh://[email protected]/user/address.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch master]
remote = origin
merge = refs/heads/master

如何以首次启动的格式输出它?

linux bash ubuntu
2个回答
2
投票

根本不要使用printf。只需使用cat的here文档:

cat <<'EOF' > config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://[email protected]/user/address.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
EOF

或者,使用内容定义变量

config='
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://[email protected]/user/address.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
'

printf '%s\n' "$config" > config

0
投票

在“\ n”之后,您可以使用“\ t”来创建标签。

© www.soinside.com 2019 - 2024. All rights reserved.