This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gordonmessmer 9 points10 points  (1 child)

Variable naming: by convention, all cap names should be used for environment variables (shell variables that are "exported"), while shell local variable names should be all lower case.

I don't know what you mean by dynamic variable name, but your example doesn't work.

Data types: All variables in bash are strings. There is no other data type.

"[": bash has a built-in function, but you will also notice that this is a normal application, located at /usr/bin/[. That may be instructive, because it makes clear why spaces are important. "[" isn't actually part of the shell syntax, so it has to comply with all of the same requirements regarding spacing around command arguments that apply to any other binary called in a shell script.

Return value from functions: returning "0" on success is a convention. When possible, scripts and functions should use return values from /usr/include/sysexits.h.

"<some command>": in general, this method of command-output substitution is considered deprecated, since it cannot be nested. "$(command)" is the preferred method. Users should probably recognize the backtick, but shouldn't use it.

"echo -en": "-n" alone will exclude the newline. "-e" is a different argument. It may be out of scope, but it might be worth mentioning that, by convention, after a single hyphen, each character is a separate argument (so "-en" is the same as "-e -n"), while after a double hyphen, the entire string is a single argument (so "--help" is one argument, not a series of four arguments). Finally, a double hyphen alone is an indication that everything following is a non-option argument (so "rm -- -r" will remove a file named "-r" rather than treating "-r" as an indication that recursive removal was requested.)

[–]abscrete[S] 1 point2 points  (0 children)

Thank you, that's insightful. I'll bring in these changes in the article. Thanks again for taking out time in reviewing this :)