sixtyfootersdude
sixtyfootersdude

Reputation: 27231

What is the proper indentation for bash scripts?

What is the proper indentation for a bash script? As a java/c++ monkey I religiously indent my code. But it seems you are not allowed to indent 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 the EOF and if you just unindented the EOF (confusing) it prints indented.

Q: What is the proper indenting for bash scripts?

Upvotes: 6

Views: 12962

Answers (4)

ghostdog74
ghostdog74

Reputation: 342649

yes you can "indent", by using <<- (see bash man page on here documents)

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

    $1
    ----------------------------------------------------------
    EOF
fi

Upvotes: 4

Dan Andreatta
Dan Andreatta

Reputation: 3711

With bash (3.2 at least) and ksh (do not know about others) you can indent the here-documents using <<-, and the leading tabs will be stripped (not spaces, only tabs), e.g.

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

Upvotes: 21

Steve Emmerson
Steve Emmerson

Reputation: 7832

Mouviciel is correct.

You can put the here-file text in a separate file if you want to preserve indentation. You would then have to handle the substitution yourself, however.

Upvotes: 0

mouviciel
mouviciel

Reputation: 67859

This is not a bash indenting problem, this is a here-file problem. The label that you specify after <<, i.e., EOF, must appear alone in a line, without leading or trailing whitespaces.

For the here-file itself, it is used as typed, indentation included.

Upvotes: 2

Related Questions