0

I am slightly confused if there is any rigid rule regarding the naming of variables in Shell Scripting. I found different answers in different locations:

Can somebody clarify on this matter? To test if there is any issue if we use Lowercase letters in variable names, I ran few codes in terminal which I found ran without any error in them.

Stephen Kitt
  • 434,908
Puneet
  • 1
  • Your first link answers your question. – jesse_b Aug 09 '20 at 13:44
  • This seems to be a duplicate, of the question that you link to. The person that wrote the 1st article dose not knew what they are writing about. There is technically no difference, however there is a convention and a standard (I can't remember), to reserve all caps for system defined environment variables. This is to prevent accidental behaviour changes in your tools. I don't know of any question asking for pros, cons, and standards. If you reword the question to ask this, then it may not be a duplicate, and would have great value. – ctrl-alt-delor Aug 09 '20 at 14:25
  • 2
    What exactly is unclear and needs clarifying? Neither text that you link to says anything about rigid rules. This is because there are none. – Kusalananda Aug 10 '20 at 07:49
  • I got confused due to this line "By convention, Unix shell variables will have their names in UPPERCASE.". But I was wondering why is it even mentioned if there are no rules. For example, it mentioned in one of the answers that Uppercase variables names might conflict with built-in shell variables, then what is this convention mentioned in first link. By all means it looks similar to variable naming in C/C++ or any other programming language. I just wanted to confirm if there is any special rule in BASH. – Puneet Aug 10 '20 at 19:12
  • 1
    Do not just answer questions in comments. If the content of your response comment would improve the question then edit the question instead. Readers shall have to read the question only, not the whole comment discussion. – Hauke Laging Aug 10 '20 at 20:35

1 Answers1

1

Environment variables and built-in shell variables introduced by the operating system, shell startup scripts, or by the shell itself etc. usually use ALL CAPITALS but that is not mandatory. The shell does not care.

To prevent your own shell script variables from possibly conflicting with these variables and overriding their values, some people argue it is good practice to use lowercase names for shell variables, but again it is not mandatory. The shell does not care.

In recent years the "use lowercase for shell variable names" proponents have gained traction but, again, the shell does not care and remains agnostic.

fpmurphy
  • 4,636
  • And the use all-caps for constants, is a miss-understanding of a coding standard from C-language. – ctrl-alt-delor Aug 09 '20 at 14:27
  • 2
    @JRFerguson There is a coding standard for the C-language that states that macros should be in ALL_CAPS, this is to allow the user of the macro to be warned of side effects. Not all macros have side effects, so it could be argued that only those with side effects should be all-caps. An example of a macro with side-effects is #define MAX(a,b) ((a>=b)?(a):(b)). Even with a ridiculous amount of brackets, it has a problem, consider int n = MAX(v++, 7); What is the value of v after using the macro, if v is initially >=7? – ctrl-alt-delor Aug 09 '20 at 14:45
  • 1
    @JRFerguson The miss-understanding comes because the only way to do consents in C is with macros e.g. #define some_constant 17. People that did not know about what else a macro could do, would see constant and macro as synonyms, and then when other moving to other languages with real constants, just switched the word macro to constant. – ctrl-alt-delor Aug 09 '20 at 14:48
  • @ctrl-alt-delor Ahh, thanks for the explanation and the nice example! – JRFerguson Aug 09 '20 at 15:29
  • 2
    @ctrl-alt-delor. Why are you polluting a question about shell variable coding conventions with C language macro conventions? And your claim that the "only way to do constants in C is with macros" is false. Look up the "const" keyword. – fpmurphy Aug 09 '20 at 16:21
  • @JRFerguson maybe these comments should be answers in their own question. However it is relevant to why some use conventions, that have a disadvantage. – ctrl-alt-delor Aug 09 '20 at 19:24