You are not logged in.
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
#!/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
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
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.
Offline
Forgot to take your pills?
Offline
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
The OPs (all of them, I guess) explicitly pointed out that
And we usually use expandtab true in our vim or nvim setting.
…
Offline
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
Offline
Thanks for the replies, and the link. They are working solutions but ...
Offline
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