简体   繁体   中英

How do you indent when creating a multi line file from terminal?

So I need a command to make the output look like this:

    [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

I have tried the following,

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

But it creates a file with these contents

[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

How can I make it wehre it outputs this in the format first stated?

Don't use printf for this at all. Just use a here document with 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

Or, define a variable with the contents

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM