Proper indentation of Bash script

27,613

Solution 1

Indenting is done to clarify your code. Indentations are usually used for loops, if statements, function definitions to make it easy to see what statements are part of that loop or part of the if statement.

Another trick to make your code more readable is adding blank lines to separate out blocks of related code. For example, separating out your menu from the rest of your code with a couple of blank lines:

clear
echo "Start A Process"

echo "*************************"
echo "1. Oxygen Level."
echo "***************************"
echo "2. Temperature ."
echo ....   #And on and on...
echo "*************************"
echo "6. Humidity %."
echo "*************************"
echo "Press A to quit."
echo "*************************"

read -p "Please make a choice:" choice2

case "$choice2" in...
...

This makes it easier to see the menu you're presenting. You could also use a Here document to eliminate the repeating echoes and quotation marks that make your menu hard to see. I'd would eliminate the over fancy stuff like the lines of asterisks. They don't add anything to the program and just make it harder to read - both code wise and execution wise:

clear

cat <<MENU
START A PROCESS
------------------
1. Oxygen Level.
2. Temperature .
3. Mars Rover Speed.
4. Satellite Distance
5. Carbon Dioxide Level
6. Humidity %.
A. Quit
------------------
MENU

read -p "Please make a choice:" choice2

case "$choice2" in...
...

Notice how using a here document makes your menu is easy to see and format.

Finally, your case statement should follow the indentation rule of keeping common lines indented, so you not only know the code that goes with the case statement, but that you also indent each choice:

case "$choice2" in
    1)
        $script/simulate start oxygen
        echo "Oxygen Level Started."
        sleep 5
        ;;
    2)
       .....
       .....
       ;;
   .....
esac

Some people will compact this a bit:

case "$choice2" in
    1)  $script/simulate start oxygen
        echo "Oxygen Level Started."
        sleep 5;;
    2)  .....
        .....;;
   .....
esac

The indentation is pretty much the same. A few lines are combined to make the case more compact and easier to read.

To help you with indentation and coding, use a program editor instead of just a text editor. A good program editor like VIM will automatically indent your code and even highlight the syntax making it easier to read. Eclipse is used for Java programming, but can be used as a text editor for shell scripts. A good editor can also help you with the way to use certain commands of statements by showing you the manpage for that command. When you press capital K in VIM, it will show you the manpage for that command.

Bash program beautification is quite tricky. However, there is a Python program that will attempt the task.

Solution 2

The correct indentation depends on the style desired, which is purely a matter of opinion. In other words, the only person who can answer your question is the mysterious woman you refer to as "she". Personally, I would say you have much bigger problems than indentation. Rather than reading the selection from stdin, you ought to take it as a command line argument. Rather than always printing a menu showing the options, you should only print them in response to -h or --help or something similar. Rather than using a string of consecutive echos, use a single heredoc to print the menu. For example:

usage() {  cat <<- EOF
    $(basename $0) option

    Start A Process, where option is one of:
    1) oxygen level
    ...
    6) Humidity %.      
EOF
}

Solution 3

Style will always be a discussion. A good starting point is Google's style guide which also provides an overview of a lot of shell-constructions.

I happen to disagree with Googles style "Indent 2 spaces. No tabs.", I prefer tabs. Some developers like to see 2 spaces, some 4, and you can change it the way you like (vi: set tabstop=3 indent=3).

How to show a menu ? You could use ONE echo like this:

   echo "Start A Process
       1. Oxygen Level.
       2. Temperature .
       3. Mars Rover Speed.
       4. Satellite Distance
       5. Carbon Dioxide Level
       6. Humidity %.

       Press A to quit."

When you have more menu's, I would consider using textfiles with the menu lines and cat the correct menu file. You can see an example at https://unix.stackexchange.com/questions/38200/ksh-styling-text-based-menu-using-stderr/115371#115371

Solution 4

This question has been treated on Unix Stack Exchange already.

There is no such thing as a standard for Bash. Usually you indent at least each level of:

  • functions
  • loops (for, while)
  • conditionals (if, case)

Some examples:

function echofoo() {
  echo foo
}

or

if true
then
  echo foo
else
  echo bar
fi

or

case "$choice" in
  1)
    echo foo
  ;;
  2)
    echo bar
  ;;
    [...]

Solution 5

Just open the file with vim editor and typing gg=G will reindent the entire file.

Share:
27,613
paul
Author by

paul

Updated on July 09, 2022

Comments

  • paul
    paul almost 2 years

    I have written a Bash script which uses cases and functions for university, but no matter where I move the scripts, she says that they are not indented properly. What is the correct indentation of this sample of the script?

    clear
          echo "Start A Process"
    echo "*************************"
    echo "1. Oxygen Level."
    echo "***************************"
    echo "2. Temperature ."
    echo "*************************"
    echo "3. Mars Rover Speed."
    echo "*************************"
    echo "4. Satellite Distance"
    echo "*************************"
    echo "5. Carbon Dioxide Level"
    echo "*************************"
    echo "6. Humidity %."
    echo "*************************"
    echo "Press A to quit."
    echo "*************************"
        read -p "Please make a choice:" choice2
    
    #Choices
    case "$choice2" in
    
    1)
    $script/simulate start oxygen
    echo "Oxygen Level Started."
    sleep 5
    ;;
    2)
    

    Is there a website or a function on VMware where I can get my code automatically indented?