What is the correct indentation for bash scripts?

What is the correct indentation for a bash script? As a java / C ++ monkey, I religiously backtrack my code. But it seems you are not allowed to backtrack this code:

#! /bin/bash

if [ $# = 0 ]
then
        # there was no arguments => just do to standard output.
        echo "there are no parameters"
else
cat << EOF
==========================================================
==========================================================
==========================================================
==========================================================
DESCRIPTION:

$1
----------------------------------------------------------

EOF
fi

When indented, it does not recognize EOF, and if you just did not specify EOF (confusing), it prints indents.

Q: What is the correct indentation for bash scripts?

+5
source share
4 answers

With bash (at least 3.2) and ksh (I don't know about others), you can backtrack here with docs using <<-, and the main tabs will be deleted (not spaces, just tabs), e.g.

if [...]; then
    cat <<-EOF
        some text
    EOF
fi
+18
source

, "", <<- (. bash )

if [ $# = 0 ]
then
        # there was no arguments => just do to standard output.
        echo "there are no parameters"
else
    cat <<-EOF
    ==========================================================
    ==========================================================
    ==========================================================
    ==========================================================
    DESCRIPTION:

    $1
    ----------------------------------------------------------
    EOF
fi
+4

This is not a bash indent problem, this is a file problem. The label you specify after <<, i.e. EOF, must be displayed separately in a line without trailing or trailing spaces.

For the file itself, it is used as a typed one, indents are included.

+2
source

Muwitzel is right.

You can put the text of the file here in a separate file if you want to keep the indent. However, you will have to handle the replacement yourself.

-1
source

All Articles