Indentation for text redirection

I would like to know if there is a solution to this problem.

In my shell script, I have code to redirect some text to a file:

if [ condition ]; then
   cat > /outputfile <<EOF
text123
text345
text456
EOF
fi

The problem is that I want textxxx to be indented in my code, however, if I do, then the redirected text will contain spaces.

Is there a way that I could easily backtrack from the code and have the output file without spaces.

+4
source share
2 answers

The standard answer is to use leading tabs (and they should be tabs, not spaces) and -before the word:

cat <<-'EOF'
        Tab-indented text
    Blank-indented text
EOF

Which (if you have one or more actual tabs before "Tab-indented") will be output:

Tab-indented text
    Blank-indented text

, sed cat:

sed 's/^[     ]*//' <<EOF
    Randomly
        Indented
  Text
EOF

:

Randomly
Indented
Text

( , , , sed. sed 's/^[[:space:]]*//', .)

, 'EOF' vs EOF , ( ). :

cat <<-EOF
        $(rm -fr *)  # Do not try this at home! (Or at work.)
        $HOME
EOF

cat <<-'EOF'
        $(rm -fr *)
        $HOME
EOF

$(...) () . , -! , . script verbatim - , , $(rm -fr *), ! $HOME ; .

+2

cat<<-EOF
   text123
EOF
+5

All Articles