正确Bash和shell脚本变量资本 [英] Correct Bash and shell script variable capitalization

查看:96
本文介绍了正确Bash和shell脚本变量资本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在许多shell脚本运行在全部大写的变量,而且我一直认为,有一个严重的误解这一点。我的理解是,按照惯例(也许是必然很久以前),环境变量在全部大写。

I run across many shell scripts with variables in all caps, and I've always thought that there is a severe misunderstanding with that. My understanding is that, by convention (and perhaps by necessity long ago), environment variables are in all-caps.

但在现代脚本环境,如猛砸,我一直prefered临时变量小写变量的约定,和大写变量的仅适用于出口(即环境变量)的。例如:

But in modern scripting environments like Bash, I have always prefered the convention of lower-case variables for temporary variables, and upper-case variables only for exported (i.e. environment) variables. For example:

#!/usr/bin/env bash
year=`date +%Y`
echo "It is $year."
export JAVA_HOME="$HOME/java"

这一直是我拿的东西。是否有任何权威的来源,同意或这种做法不同意,或者是纯粹的风格问题?

That has always been my take on things. Are there any authoritative sources which either agree or disagree with this approach, or is it purely a matter of style?

推荐答案

按照惯例,环境变量( PAGER EDITOR ,..)和内部shell变量( SHELL BASH_VERSION ,...)都大写。所有其他变量名应该是小写。

By convention, environment variables (PAGER, EDITOR, ..) and internal shell variables (SHELL, BASH_VERSION, ..) are capitalized. All other variable names should be lower case.

请记住,变量名是区分大小写的;此约定可以避免意外重写环境和内部变量。

Remember that variable names are case-sensitive; this convention avoids accidentally overriding environmental and internal variables.

饲养这个约定,你可以放心,你不需要知道,以避免覆盖它们使用的UNIX工具或贝壳每一个环境变量。如果这是你的变量,它小写。如果导出它,它大写

Keeping to this convention, you can rest assured that you don't need to know every environment variable used by UNIX tools or shells in order to avoid overwriting them. If it's your variable, lowercase it. If you export it, uppercase it.

这篇关于正确Bash和shell脚本变量资本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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