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:
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
#
# ...
# ...
# ...
#
################################################################################
$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.
$$ 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.
$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.
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.
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.
A foolish consistency is the hobgoblin of little minds, adored by little statesmen and philosophers and divines.
– Ralph Waldo Emerson
-execdir
when appropriate with find
.null
separators when the toolset allows to avoid accidentally wordsplitting on whitespace.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