在Bash中命名常量的约定是什么? [英] What is a convention for naming a constant in Bash?

查看:129
本文介绍了在Bash中命名常量的约定是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在shell脚本中,即使我使用JavaPython样式命名约定,我仍然不清楚如何命名常量.

许多约定建议我使用大写字母"和下划线"来命名常量,例如MY_CONSTANTPI.但是在Bash中,这可能与环境变量冲突. /p>

那么,Bash常数的正确命名约定是什么?

解决方案

好问题!

与您要链接的问题一起,在Unix& amp;中还有另一个相关的问题. Linux: shell脚本中的变量有命名约定吗?.

您可以找到几个很好的答案:

由操作系统引入或启动的变量 脚本等通常都在CAPITALS中,这些被称为 "环境变量".

为防止您自己的变量与环境冲突 变量,使用lower case是一个好习惯.

连同 Shell样式指南链接,您可以在其中找到:

命名约定

函数名称

▶小写,带有下划线以分隔单词.单独的库 和 ::.函数名称后必须带括号.关键字 函数是可选的,但必须在整个过程中始终使用 项目.

变量名

▶至于功能名称.

常量和环境变量名称

▶所有大写字母,用下划线隔开,声明在 文件.

man bash中没有定义的标准,因此您最好牢记以下基本思想:大写.

In shell scripting, even though I use Java or Python style naming convention, I am still unclear about naming a constant.

Many conventions suggest me to use "capital letter" together with "underscore" for naming a constant e.g. MY_CONSTANT, PI. But in Bash, this may conflict with environment variables.

So, what is the right naming convention for Bash constants?

解决方案

Good question!

Together with the question you are linking, there is another related question in Unix & Linux: Are there naming conventions for variables in shell scripts?.

There you can find a couple of good answers:

Variables that are introduced by the operating system or start up scripts etc. are usually all in CAPITALS, these are called 'envrironment variables'.

To prevent your own variables from conflicting with environment variables, it is a good practice to use lower case.

Together with a Shell Style Guide link, where you can find:

Naming Conventions

Function Names

▶ Lower-case, with underscores to separate words. Separate libraries with ::. Parentheses are required after the function name. The keyword function is optional, but must be used consistently throughout a project.

Variable Names

▶ As for function names.

Constants and Environment Variable Names

▶ All caps, separated with underscores, declared at the top of the file.

There is no defined standard in man bash, so you better use your our convention bearing in mind the basic idea: be careful with uppercase.

这篇关于在Bash中命名常量的约定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆