Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash coding conventions

Tags:

bash

I am trying to learn some Bash to maybe get a job working with computers one day.

To improve my clarity and disciple writing my self-learning code, I am trying to adhere to a set of consistent "guiding principles".

As I roll my own "guidelines" I obviously ask myself: should I not be using an established standard instead?

I could not find one such "authoritative" reference for Bash, similar to what these other languages have:

  • Java (http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html) How to Write Doc Comments for the Javadoc Tool
  • Java (http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html) Code Conventions for the Java Programming Language
  • Java (https://code.google.com/p/java-coding-standards/wiki/Introduction) Google Java coding standards
  • Python (http://www.python.org/dev/peps/pep-0008/) PEP 8 -- Style Guide for Python Code
  • Python (http://google-styleguide.googlecode.com/svn/trunk/pyguide.html) Google Python Style Guide

Is there a link with a similar document for Bash which has good reason for being used?

Here is the type of stuff I was putting together on my own... but I think, especially as a beginner, I should be using guidelines written by experts rather than trying to come up with my own, as they would not be based on much experience, insight, practicality, knowledge of common patterns/anti-patterns, etc.

You may dispute the validity of such documents in general, but some people must like them for the web to have such prominent examples online as the ones I mention in the bullet-list above..


################################################################################    
# Coding conventions                                                                
#                                                                                   
# - Prefer lines of 80 characters of length or less                                 
#                                                                                   
# - Perform arithmetic operations and numeric comparisons within "(( ))" blocks     
#   e.g. if ((42<=24+24)), ((3**3==27))                                             
#                                                                                   
# - Reference variables by name, not expansion, within arithmetic evaluation        
#   e.g. ((i++)) rather than (($i++)), ((v+=42)) rathern than v=$(($v+42))          
#                                                                                   
# - Prefer "[[" to "[" for conditional expressions                                  
#                                                                                   
# - Prefer "[[ $s ]]" to "[[ -n $s ]]" when checking for empty strings              
#                                                                                   
# - Document each function with at least a summary sentence. This should not        
#   exceed the preferred line length, be written in third person, end with a        
#   period and concisely describe the general utility of the function   
#
# ...
# ...
# ...
#            
################################################################################    

like image 845
Robottinosino Avatar asked Mar 25 '13 08:03

Robottinosino


People also ask

What does [- Z $1 mean in Bash?

$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.

What does $$ in Bash mean?

$$ is the process ID (PID) of the script itself. $BASHPID is the process ID of the current instance of Bash. This is not the same as the $$ variable, but it often gives the same result. Copy link CC BY-SA 3.0.

What is $1 and $2 in shell script?

$0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1) $9 is the ninth argument.

Is it true 1 or 0 Bash?

There are no Booleans in Bash. However, we can define the shell variable having value as 0 (“ False “) or 1 (“ True “) as per our needs.


2 Answers

As usual, Google is your friend. The best bash style guide I have come across is from Google.

There is also some additional advice from the Chromium project.

For learning bash, the Apple Developer Shell Scripting primer is excellent.

Using a style guide for bash is rather sensible, as it is full of tricks and unexpected and difficult to diagnose quirks. Thus, if you follow the same style all the time, you should only fall for each of those tricks once.

like image 142
oenpelli Avatar answered Sep 18 '22 15:09

oenpelli


My shell scripting standards

  1. Prefer portability, but don't sacrifice security and whitespace awareness to it.
  2. Prefer builtins over external commands.
  3. But, use fast external commands to process very large inputs.
  4. Avoid unnecessary subshells and pipelines.
  5. Don't preoptimize.
  6. Learn the rules of quoting. Then, use quotes.
  7. Use functions to improve readability and control scope.
  8. Don't give scripts silly file extensions.
  9. Never change directory without checking that it worked.
  10. Eschew hobgoblins

A foolish consistency is the hobgoblin of little minds, adored by little statesmen and philosophers and divines.

– Ralph Waldo Emerson

When to ignore the portability rule

  1. Use -execdir when appropriate with find.
  2. Use null separators when the toolset allows to avoid accidentally wordsplitting on whitespace.
  3. Learn all the glob extensions and use them.
  4. If your target systems all have BASH, don't bend over backwards to be POSIXLY_STRICT.
  5. Use here strings.
like image 44
kojiro Avatar answered Sep 21 '22 15:09

kojiro