Bash 编码约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15610794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash coding conventions
提问by Robottinosino
I am trying to learn some Bash to maybe get a job working with computers one day.
我正在尝试学习一些 Bash,以便有一天能找到一份使用计算机工作的工作。
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:
我找不到 Bash 这样的“权威”参考,类似于这些其他语言所具有的:
- 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
- Java ( http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html) 如何为 Javadoc 工具编写文档注释
- Java ( http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html) Java 编程语言的代码约定
- Java ( https://code.google.com/p/java-coding-standards/wiki/Introduction) Google Java 编码标准
- Python ( http://www.python.org/dev/peps/pep-0008/) PEP 8 -- Python 代码风格指南
- Python ( http://google-styleguide.googlecode.com/svn/trunk/pyguide.html) Google Python 风格指南
Is there a link with a similar document for Bash which has good reason for being used?
是否有一个带有类似 Bash 文档的链接,它有充分的理由被使用?
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 somepeople 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
#
# ...
# ...
# ...
#
################################################################################
采纳答案by kojiro
My shell scripting standards
我的 shell 脚本标准
- Prefer portability, but don't sacrifice security and whitespace awareness to it.
- Prefer builtins over external commands.
- But, use fast external commands to process very large inputs.
- Avoid unnecessary subshells and pipelines.
- Don't preoptimize.
- Learn the rules of quoting. Then, use quotes.
- Use functions to improve readability and control scope.
- Don't give scripts silly file extensions.
- Never change directory without checking that it worked.
- 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
何时忽略可移植性规则
- Use
-execdirwhen appropriate withfind. - Use
nullseparators when the toolset allows to avoid accidentally wordsplitting on whitespace. - Learn all the glob extensions and use them.
- If your target systems all have BASH, don't bend over backwards to be POSIXLY_STRICT.
- Use here strings.
- 使用
-execdir时,用适当的find。 null当工具集允许避免在空白处意外分词时,请使用分隔符。- 学习所有的 glob 扩展并使用它们。
- 如果你的目标系统都有 BASH,不要向后弯腰成为 POSIXLY_STRICT。
- 在这里使用字符串。
回答by oenpelli
As usual, Google is your friend. The best bash style guide I have come across is from Google.
像往常一样,谷歌是你的朋友。我遇到的最好的 bash 风格指南来自 Google。
There is also some additional advice from the Chromium project.
还有一些来自 Chromium 项目的额外建议。
For learning bash, the Apple Developer Shell Scripting primeris excellent.
对于学习 bash,Apple Developer Shell Scripting 入门非常棒。
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.
使用 bash 风格指南是相当明智的,因为它充满了技巧和意想不到且难以诊断的怪癖。因此,如果您一直遵循相同的风格,那么您应该只对这些技巧中的每一个上瘾一次。

