As far as I've seen there are two ways to initialize a variable with the output of a process. Is there any difference between these two?
ex1=`echo 'hello world'`
ex2=$(echo 'hello world')
Two types of variable initialization exist: explicit and implicit. Variables are explicitly initialized if they are assigned a value in the declaration statement. Implicit initialization occurs when variables are assigned a value during processing.
The way of initializing a variable is very similar to the use of PARAMETER attribute. More precisely, do the following to initial a variable with the value of an expression: add an equal sign (=) to the right of a variable name. to the right of the equal sign, write an expression.
Java offers two types of initializers, static and instance initializers.
You get same effect.
The $()
is recommended since it's more readable and makes it easier to nest one $()
into another $()
.
Update:
The $()
syntax is a POSIX 1003.1 standard (2004 edition). However, on some older UNIX systems (SunOS, HP-UX, etc.) the /bin/sh
does not understand it.
You might need to use backtick "`" instead or use another shell (usually it's ksh) if you need your script to work on such environment.
If you don't know which syntax to use - use $()
. Backtick syntax is deprecated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With