Bash shell script syntax style hell


#1

I don’t normally develop bash shell scripts but have been lately. The automatic indenting is driving me insane. Some times it works, but most times it assumes some vicious coding style from a time when programmers lived in caves and ate their young. It appears to enforce this archaic format:

if [ -f some_file ]; then
   do something
fi

So when I try to use the modern style all new scripts should be written with:

if [ -f some_file]
then
    do something
fi

It shoves the then over to the “do something” alignment". Then when I type the fi it un-indents it. Turning autoindent off means I have to tab everything in myself.

Is there a way to change this functionality? I looked for some configurable language- package, but they all seem to simply allow disabling.


#2

This is actually super helpful - I hadn’t realized till now that there was a newer (and much saner) style for Bash scripts :sweat_smile:. I’d personally welcome tweaks to the Bash language grammar - would you mind opening an issue there?


#3

Putting an opening brace (then) on the same line as the conditional is a common style choice. I would have it no other way. Do you code your C syntax like this?

  if (x==y)
  {
    do(something);
  }

In any case you are being a bit judgmental about something that is clearly a style preference.


#4

Yes mnquintana there is a much saner style enforced in most shops for bash shell scripts.

@mark_hahn:

I would code it like this:

if (x == y)
{
    do( something);
}

which is the modern style guide enforced in most shops. Note the required spaces on each side of == and the required space between the ( and first parameter to a function/method.

Ancient legacy stuff from more than 30 years ago used to use ]; then and used to code C in K&R style. There were no style guides. The “style” was adopted because of the mechanical keyboards and the cost of storage. The mechanical keyboards of the day required significant strength. It wasn’t until the early LA series paper terminals where typing became close to what we know today. The VT-52, predecessor of the VT-100 made typing a dream . . . once the keyboard got broken in a bit.

Style guides enforced by shops must be adhered to. I did not choose the style guide, but I can be very choosy about clients I work for, so one of the initial questions I have is to see the style guide. If it is archaic, I don’t care what they are paying, I don’t take the contract.

Most of today’s editors have some syntax/style adjustment settings which allow a shop to configure and enforce a specific style on code. I was simply asking how to adjust the bash shell script settings.


#5

I hope you don’t mind I edited your post for clarity.


#6

I’ve never seen a style guide for bash. Can you show me one?

The only style guide I’m familiar with, that is enforced, is in GO. Note that is uses your “vicious” format …

if err != nil {
    // error handling
} else {
    // normal code
}

The main reason I use this style is to fit more code on a screen. This has and will be a factor to consider forever.

Edit: The snippet of go I showed is from the GO repo.


#7

Since the original question was answered by @mnquintana above, rather than dwelling on what style preferences we each have, I feel like a more productive discussion is what kinds of common style variations are there in various languages so that a “style engine” could be defined for Atom. Currently, the indentation style (and nothing else, I think) is nudged (not actually enforced) by the language definition. I’d like to see that abstracted out so that the language definition maybe contains a default … but one could configure the various style choices easily.


#8

I will shut up after this last argument …

Google is a big shop right? This is from their official bash style guide …

for dir in ${dirs_to_cleanup}; do
  if [[ -d "${dir}/${ORACLE_SID}" ]]; then
    log_date "Cleaning up old files in ${dir}/${ORACLE_SID}"
    rm "${dir}/${ORACLE_SID}/"*
    if [[ "$?" -ne 0 ]]; then
      error_message
    fi
  else
    mkdir -p "${dir}/${ORACLE_SID}"
    if [[ "$?" -ne 0 ]]; then
      error_message
    fi
  fi
done

Edit:

Actually he withdrew his answer. I don’t know why.


#9

Oh, you are more than welcome to clarify it.


#10

The answer was essentially that Atom currently has indentation “assistance” that is supplied by the language package. @mnquintana suggested that you could open an Issue on the language-shell package to have this behavior changed for this particular construct to be more amenable. The defaults for all languages tends to favor the K&R brace style. This is a known limitation to the way Atom is currently built. Rather than one-offing all of these individual style choices … I think it would be better if everyone’s energy is directed into fixing the problem across all languages and styles.


#11

I have no idea why mnquintana withdrew the post. It was polite and to the point. I even logged a bug/issue as suggested.

Well, you’ve went from never seeing one to posting about 2 different ones in a short span this afternoon so they do exist. I cannot post the ones I’m using for clients as they are covered under NDA are the bulk of the projects.

My question has been answer. There is no good style in atom and no ability to force good style. As leedohm pointed out there is no style engine so my question has been answered a second time.


#12

There is the atom-beautify package though it is, to my knowledge, a post-formatter not as-you-type.


#13

Ah, the power of Google search. (grin)