Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brackets ${}, $(), $[] difference and usage in bash

Tags:

bash

Bash contains different braces and they used differently. Could you please explain comprehensively what is difference between ${}, $() and $[]? Where they are used in bash? What is their main purpose?

like image 703
yart Avatar asked Aug 01 '13 11:08

yart


People also ask

What is $[] in bash?

$[] is a deprecated syntax for arithmetic expansion. It is thoroughly replaced by $(()) – chepner.

What does ${} mean in bash?

$() means: "first evaluate this, and then evaluate the rest of the line". Ex : echo $(pwd)/myFile.txt. will be interpreted as echo /my/path/myFile.txt. On the other hand ${} expands a variable.

What does brackets mean in bash?

<( Angle Parentheses ) Meaning that you can do things like: diff two streams. run a command within a shell to create an input-"file" for other commands that want input in the form of a file rather than a stream.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script.


1 Answers

The keyword is shell expansion! So i highly recommend you to read the section Shell Expansions in your bash reference manual.

You can access this section online [1].

The names of the following stated forms are:

  • ${} Parameter expansion
  • $() Command substitution

the form $[] isn't actually stated in the manual so i wouldn't use it.

[1] http://www.gnu.org/software/bash/manual/bashref.html#Shell-Expansions

like image 193
user1146332 Avatar answered Sep 28 '22 19:09

user1146332