Bash convention for if ; then

后端 未结 5 778
野趣味
野趣味 2021-02-15 11:42

From this web page :

http://tldp.org/LDP/abs/html/abs-guide.html

It\'s mentioned the usage of the if bracket then convention which need a space after the semicol

5条回答
  •  清歌不尽
    2021-02-15 12:14

    This has become the style in the last few years:

    if [ -x "$filename" ]; then
       echo "hi"
    fi
    

    However, back when dinosaurs like Burroughs and Sperry Rand ruled the earth, I learned to write if statements like this:

    if [ -x "$filename" ]
    then
        echo "hi"
    fi
    

    Then, you don't even need a semicolon.

    The new style with then on the same line as the if started in order to emulate the way C and other programming languages did their if statements:

    if (! strcmp("foo", "bar")) {
       printf "Strings equal\n";
    }
    

    These programming languages put the opening curly brace on the same line as the if.

提交回复
热议问题