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?
Use good indentationIndentation makes it easy to see the basic form of your script's logic. It doesn't matter much whether how many space you indent, though most people seem to use 4 spaces or 8. Just make sure that your do's and done's line up and you'll be fine.
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
A basic Bash script has three sections. Bash has no way to delineate sections, but the boundaries between the sections are implicit. All scripts must begin with the shebang (#!), and this must be the first line in any Bash program. The functions section must begin after the shebang and before the body of the program.
Bash indenting is very sensitive to characters. For example a space behind “do” in while/for loops will throw it of. When you have nested loops this is very ugly, and makes it hard to follow the code.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With