bashstyle icon indicating copy to clipboard operation
bashstyle copied to clipboard

Use curly braces for all variable references

Open nikolay opened this issue 9 years ago • 6 comments

How about standardizing on braces?

# Yep
echo "${var}"
echo "$1"

# Nope
echo "$var"

nikolay avatar Jan 14 '15 22:01 nikolay

This is a somewhat common convention that I've not yet adopted. I understand the argument for consistency and avoiding surrounding text from being interpreted as part of the name. It's just one of those things that's so different from de facto Bash that I'm hesitant to force it upon people, including myself.

I'll leave this open and if I ever use it in a project and it feels like I shouldn't go back, I'll adopt it here. Thanks for bringing this one up.

progrium avatar Jan 14 '15 22:01 progrium

Yeah, I've found that this is generally overkill, since I'm practically never expanding a variable next to a character that's a valid identifier character. I prefer curly-brace expansion as a signifier of "I'm doing something crazy with this variable" like replacement or substringing.

But, then, I use backticks for substituting non-nested commands, so maybe I'm just lazy / character-miserly.

stuartpb avatar Jan 15 '15 00:01 stuartpb

It adds unnecessary visual noise, and is not required for simple variable interpolation most of the time.

It's only really required when you need to stand off the variable name from a string suffix, e.g., "${base}_suffix.ext", or "${RED}ERROR!$RESET".

But…

…when you list off all the other times when braces are required around a variable name—parameter expansion (${param:+default}) and arrays ("${array[@]}")—I can definitely start to see where you're coming from, from the perspective of consistency.

ernstki avatar Oct 18 '20 13:10 ernstki

Conventions are about picking the best way among multiple options by the spec.

nikolay avatar Oct 19 '20 00:10 nikolay

my two cents: I like to not waste time thinking about syntax/styles while coding. In this context, I mean I prefer to always use ${curly_braces} for consistency and just to avoid ":thinking: should I use curly braces here?"

meleu avatar Oct 20 '20 01:10 meleu

@meleu Yup! Conventions are not for the lazy but for the consistent.

nikolay avatar Oct 20 '20 06:10 nikolay