Command inside $ () in bash script

I am new to Linux. I see a bash command (is that even the correct term?) That sets the JAVA_HOME environment variable on the command line:

 export JAVA_HOME =$(readlink -f /usr/bin/java |sed "s:bin/java::") 

I know what the command does inside $() . But why do you need $() ? This failed if I did not turn it on.

Obviously googling $() does not work very well.

+4
source share
3 answers

He used to readlink . For instance:

 cnicutar@lemon :~$ os=$(uname) cnicutar@lemon :~$ echo $os Linux 
+3
source

$() is called command substitution. It replaces the output of the command with the command itself. There are two main ways to replace commands:

 $(command) 

or with backticks

 `command` 

The first option is preferred.

Read more about replacing teams here .

+4
source

The expression $(...) executes the command and replaces the output of the command. Try something like this:

 echo $(date) 

So, in this example, he takes the output of the readlink command and assigns it to JAVA_HOME (after running through sed ).

Take a look at the bash man page for more details.

+1
source

Source: https://habr.com/ru/post/1411173/


All Articles