Difference between braces {} and brackets () in shell scripting
Asked Answered
M

2

9

We use braces {} for variable expression like

NAME="test"

FILE_NAME=${NAME}file

But I don't understand in which scenarios we use brackets () Say nslookup $(hostname) works only with () brackets.

Can someone explain?

Murmurous answered 7/5, 2015 at 16:27 Comment(1)
In US English, these: () are parentheses, and these: [] are brackets.Cambyses
E
14

Minor nitpick first:

  • Brackets []
  • Parentheses ()
  • Braces {}
  • (Double) Quotation marks ""
  • (Single) Quotation marks (apostrophes) ''
  • Backticks `` (Same as the tilde ~ key)

Braces are used in BASh scripts for complex variable expansion. Consider string concatenation:

STR="hello"
STR2=$STR

STR2 evaluates to "hello". What if you wanted to make it something like "helloWorld". Doing something like STR2="$STR2World" won't work, so you use braces, ie: STR2="${STR}World".

As for brackets, they are used, similar to the backtick, `, which expands the text between them as the text output from a command.

What if you wanted to store the current time as a string?

STR2=$(date)

Now STR2 stores the string "Thu May 7 09:32:06 PDT 2015".

Additionally, you can use parentheses to execute something in a subshell, which will potentially affect your environment, PID, etc. Very useful for cases where you want a "throwaway" environment with having to track/restore environment variables, directories via pushd/popd instead of cd, etc.

Emboss answered 7/5, 2015 at 16:32 Comment(8)
I believe these () are commonly called "brackets" in UK English.Cambyses
@KeithThompson From what I've found, you appear to by correct. Parentheses are called brackets, or to be more explicit, "circle brackets", while [] are always explicitly called "square brackets" in UK English. separatedbyacommonlanguage.blogspot.ca/2006/08/…Emboss
If you are gonna nitpick then backticks aren't the same as the tilde key on all keyboard layouts. Not every programmer runs an English keyboard, seeing as many countries have their own standard.Heda
@Heda Given that English is the lingua franca on all SE sites, I think it's fair by extension to assume English language keyboards.Emboss
It actually isn't since English is an international language. Every latin-based language (that I know of) can type English without the need to swap keyboards.Heda
@Heda We should start a meta post on US/101 keyboards (and their derivatives) being de facto standards amongst the primary SO audience, or at least the largest cross-section of the SO community.Emboss
I find it a little easier to think of ${foo} as the canonical way to expand parameters, and that $foo is an example of a special case where the braces can be dropped it doing so doesn't create an ambiguity.Antipodes
@Antipodes Good point. I personally use the brace-notation you described as the default out of habit. Easier to code when the style is consistent.Emboss
P
5

Using parentheses ( executes something. There happens to be a program named hostname - so $(hostname) will execute it.

try which hostname to see where that program resides

Pradeep answered 7/5, 2015 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.