DEV Community

Discussion on: Bash Brackets Quick Reference

Collapse
 
kogans profile image
Stanislav Kogan

It's nice to show off some tricks, but the amount of bad and downright DANGEROUS practices in this article is just too much. Really, sometimes it's just best to READ THE F-CKING MAN PAGE!!!

Collapse
 
rpalo profile image
Ryan Palo

Hi there! I found the man pages dense and opaque at times, which is why I put this article together. Are there any especially dangerous practices that you’d like to point out?

Collapse
 
kogans profile image
Stanislav Kogan

It's true that bash man page is a very dry reading, but once you get the basics figured out, it is definitely the best source. That's why it's important to use the correct terminology in your article - it will allow people to quickly find the necessary subjects in the man page.

Now, for the mistakes in the article.

  • "(( Double Parentheses ))" is actually a compound command that undergoes arithmetic evaluation. its the equivalent of "let".
# Not correct
a=(( 4 + 1 ))

# Correct
((a=4 + 1))
Enter fullscreen mode Exit fullscreen mode
  • "$( Dollar Single Parentheses )" is actually called "Command substition"

  • "$( Dollar Single Parentheses Dollar Q )$?" is an absoultely HORRIBLE trick, NEVER EVER EVER do this! Oh, and btw, this doesn't actually work, because, the few times that it won't spew out an error, it will always evaluate to true.

# This is very bad
if [[ $( grep -q PATTERN FILE )$? ]]; then
 ...

# Do this instead
if grep -q PATTERN FILE; then
 ...

# oh, and by the way
[[ $(false)$? ]]; echo $?
#=> 0
# thats because  $? is evaluated as a string, and "1" is non empty

[[ $(false) $? ]]; echo $?
#=> bash: conditional binary operator expected
#=> bash: syntax error near `$?'
# LOL
Enter fullscreen mode Exit fullscreen mode
  • "$(( Dollar Double Parentheses ))" is actually called "Arithmetic expansion" with the expression within undergoing "Arithmetic evaluation". An interesting fact: double paratheses is not the only place arithmetic evaluation occurs: it also occurs inside array subscripts, which can be a pretty surprising thing sometimes.

  • [ Single Square Brackets ]. This deserves a special caution: unless you require backward compatibility with classic sh, you should NEVER use it. It's error-prone, requires gillion of quotes to make reliable and spawns a separate process for no good reason. Also, "[[" has more features.

  • "{ Single Curly Braces }" is called "brace expansion".

  • "${dollar braces}" is called "parameter expansion".

Thread Thread
 
rpalo profile image
Ryan Palo

Yep, I’m aware of what these are all called. I wrote it this way because the target audience most likely wouldn’t, and I wanted to make it an accessible visual guide. That being said, your other notes are helpful, and, when I get a chance, I’ll do my best to add the warnings and caveats where they’re needed. Thanks for taking the time to walk me through your experiences!

Some comments have been hidden by the post's author - find out more