You are not logged in.

#1 2023-03-20 05:33:11

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

[SOLVED] using EOF in bash with indent

I am trying to write some content to a file using EOF in bash script.

#!/bin/bash

if [[ $1 == "yes" ]]; then
    cat <<-EOF > ./eofoutput.txt
    this is testing EOF with indent
    this is testing EOF with indent
    these lines wanted to be without leading indent
    these lines wanted to be without leading indent
    EOF
fi

returns the following error output

./testeof: line 10: warning: here-document at line 4 delimited by end-of-file (wanted `EOF')
./testeof: line 11: syntax error: unexpected end of file

As far as I know, that is because of leading space/tab of EOF. So the following code

#!/bin/bash

if [[ $1 == "yes" ]]; then
    cat <<-EOF > ./eofoutput.txt
    this is testing EOF with indent
    this is testing EOF with indent
    these lines wanted to be without leading indent
    these lines wanted to be without leading indent
EOF
fi

works with no error. But the output lines are with leading tab as follows;

    this is testing EOF with indent
    this is testing EOF with indent
    these lines wanted to be without leading indent
    these lines wanted to be without leading indent

I want without leading tab/space as follow;

this is testing EOF with indent
this is testing EOF with indent
these lines wanted to be without leading indent
these lines wanted to be without leading indent

How can I do that. I googled a lot. Many forum mentioned <<-EOF to ignore leading tab/space. But it doesn't work.
Please help me. Thanks in advance.

Last edited by duyinthee (2023-03-20 10:08:47)

Offline

#2 2023-03-20 08:07:59

fennec001
Member
Registered: 2023-02-17
Posts: 7

Re: [SOLVED] using EOF in bash with indent

#!/bin/bash

if [[ $1 == "yes" ]]; then
    cat <<-EOF > ./eofoutput.txt
this is testing EOF with indent
this is testing EOF with indent
these lines wanted to be without leading indent
these lines wanted to be without leading indent
EOF
fi

eofoutput.txt:

this is testing EOF with indent
this is testing EOF with indent
these lines wanted to be without leading indent
these lines wanted to be without leading indent

Offline

#3 2023-03-20 09:04:04

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

Re: [SOLVED] using EOF in bash with indent

Yes, thanks. It works but in scripts, we have indents and indents a lot.
And we usually use expandtab true in our vim or nvim setting.
In that case, when we write a script like this;

#!/bin/bash

if [[ $1 == "yes" ]]; then
    cat <<-EOF > ./eofoutput.txt
    this is testing EOF with indent
    this is testing EOF with indent
    these lines wanted to be without leading indent
    these lines wanted to be without leading indent
    EOF
fi

it does not work.

Offline

#4 2023-03-20 09:31:50

seth
Member
Registered: 2012-09-03
Posts: 49,968

Re: [SOLVED] using EOF in bash with indent

I am trying to write some content to a file using EOF in bash script.

we have indents … we usually use expandtab … when we write a script

Forgot to take your pills?

Either way, you're inserting a file here, the indentation is part of that.
If you want to use indentation but not see it reflected in the output, use echo or printf.

Online

#5 2023-03-20 10:08:24

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

Re: [SOLVED] using EOF in bash with indent

Forgot to take your pills?

Offline

#6 2023-03-20 12:19:16

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] using EOF in bash with indent

FWIW, and this may be obvious, but this works just as you'd want so long as you use tabs not spaces.

Just add a vim line to prevent this setting converting the tabs to spaces if needed.

Last edited by Trilby (2023-03-20 12:20:20)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2023-03-20 13:46:53

seth
Member
Registered: 2012-09-03
Posts: 49,968

Re: [SOLVED] using EOF in bash with indent

The OPs (all of them, I guess) explicitly pointed out that

And we usually use expandtab true in our vim or nvim setting.

hmm

Online

#8 2023-03-20 14:03:41

seth
Member
Registered: 2012-09-03
Posts: 49,968

Re: [SOLVED] using EOF in bash with indent

Bump to alert the OP, the subject is surprisingly popular and ppl. came up w/ fugly but working solutions indeed:
https://stackoverflow.com/questions/338 … ith-spaces

Online

#9 2023-03-21 04:02:03

duyinthee
Member
Registered: 2015-06-14
Posts: 222
Website

Re: [SOLVED] using EOF in bash with indent

Thanks for the replies, and the link. They are working solutions but ...

Offline

#10 2023-06-08 02:43:59

solskog
Member
Registered: 2020-09-05
Posts: 407

Re: [SOLVED] using EOF in bash with indent

They are working solutions but ...

b.eof ()
{
    local variable=a;
    \builtin readarray msg <<-EOF
        line_with_indent $variable
        @line_without_indent
EOF
    printf "%s" "${msg[@]#    }" | /usr/sbin/sed -e "s;^  *@;;"
}

$ b.eof
    line_with_indent a
line_without_indent

Offline

Board footer

Powered by FluxBB