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

时间:2017-12-22 13:58:14

标签: linux bash ubuntu

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

    [core]
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
    [remote "origin"]
            url = ssh://git@github.com/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://git@github.com/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://git@github.com/user/address.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch master]
remote = origin
merge = refs/heads/master

如何以第一次说明的格式输出它?

2 个答案:

答案 0 :(得分:2)

根本不要使用printf。只需使用cat

的此处文档即可
cat <<'EOF' > config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://git@github.com/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://git@github.com/user/address.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
'

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

答案 1 :(得分:0)

&#34; \ n&#34;你可以使用&#34; \ t&#34;创建标签。