Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different ways to initialize a variable

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')
like image 711
Paul Manta Avatar asked Dec 18 '11 21:12

Paul Manta


People also ask

What are the two ways of initializing variables?

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.

How do you initialize variables?

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.

What are the two ways of initializing variables in Java?

Java offers two types of initializers, static and instance initializers.


1 Answers

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.

like image 148
Michał Šrajer Avatar answered Oct 17 '22 15:10

Michał Šrajer