Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use curly braces for all variable references #24

Open
nikolay opened this issue Jan 14, 2015 · 6 comments
Open

Use curly braces for all variable references #24

nikolay opened this issue Jan 14, 2015 · 6 comments

Comments

@nikolay
Copy link

nikolay commented Jan 14, 2015

How about standardizing on braces?

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

# Nope
echo "$var"
@progrium
Copy link
Owner

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 progrium changed the title Variable referencing Use curly braces for all variable references Jan 14, 2015
@stuartpb
Copy link

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.

@ernstki
Copy link

ernstki commented Oct 18, 2020

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.

@nikolay
Copy link
Author

nikolay commented Oct 19, 2020

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

@meleu
Copy link

meleu commented Oct 20, 2020

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?"

@nikolay
Copy link
Author

nikolay commented Oct 20, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants