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')

-------------Problems Reply------------

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.

see http://mywiki.wooledge.org/BashFAQ/082

also notice that $() is POSIX so it does work on sh.

There is another way to initialize a variable to a default one if you haven't initialized it yourself.

[jaypal:~/Temp] a="I have initialized var a"
[jaypal:~/Temp] echo ${a:="Default value"}
I have initialized var a
[jaypal:~/Temp] a=
[jaypal:~/Temp] echo ${a:="Default value"}
Default value

Category:bash Views:0 Time:2011-12-18

Related post

  • C: Initialize module variable 2009-11-25

    I got two modules (compile units), both using a module variable with the same name: FileA.c and FileB.c both contain: #includes int m_Test; // Functions That's no problem, both variables are independent, as expected - but as soon as I assign values t

  • How to default-initialize local variables of built-in types in C++? 2010-04-06

    How do I default-initialize a local variable of primitive type in C++? For example if a have a typedef: typedef unsigned char boolean;//that's Microsoft RPC runtime typedef I'd like to change the following line: boolean variable = 0; //initialize to

  • Is it bad practice to initialize a variable to a dummy value? 2010-05-05

    This question is a result of the answers to this question that I just asked. It was claimed that this code is "ugly" because it initializes a variable to a value that will never be read: String tempName = null; try{ tempName = buildFileName(); } catc

  • How to initialize several variables in a for (;;) loop in C? 2010-07-27

    I thought one could initialize several variables in a for loop: for (int i = 0, char* ptr = bam; i < 10; i++) { ... } But I just found out that this is not possible, gcc gives the following error: error: expected unqualified-id before 'char' Is it

  • Default initialization of variables in action forms 2010-07-31

    i have made a from like : MyForm extend ActionForm{ list<Menu> MenuList=null; MyForm(){ super(); setMenuList(); //initialize menu list } } Menu object has string desciption and boolean variable for selected or not. In web page, i am iterating t

  • Why should I use constructor if I can initialize instance variable another way 2010-10-13

    Why should I use constructor to initialize instance variables while it is possible at the point of their declaration? class Frog { public int x = 4; Frog() { // Why should I use you } } --------------Solutions------------- You should use me because I

  • Initialize reference variables for many objects? 2010-10-15

    I'm having a little trouble building the grids for a Battleship game for my Java class. So far, I can easily make a for loop to add JPanel or JButton objects to the JFrame. However, my issue is that I'll need to use those Panels or Buttons again when

  • Why can't I initialize readonly variables in a initializer? 2010-12-16

    Why can't I initialize readonly variables in a initializer? The following doesn't work as it should: class Foo { public readonly int bar; } new Foo { bar=0; }; // does not work Is this due to some technical limits of the CLR? EDIT I know that new Foo

  • What is the better approach to initialize class variables? 2011-03-16

    Here are two way to initialize class variables. 1st Method class Test { private $var1; private $var2; public function Test($var1,$var1) { $this->var1 = $var1; $this->var2 = $var2; } } $objTest = new Test("value1","value2"); 2nd Method class Tes

  • Initialize two variables on same line 2011-05-14

    I'm having trouble finding an authoritative example or discussion of this concept. If I have 2 variables in my Ruby method that are numbers, and I need to initialize them to zero. They will be used as counters. Is this OK or safe? It works in my test

  • C# - Initialize a variable without knowing what its going to be 2011-06-28

    I have two different tables in my database, and each are displayed to the user based on their "SortOrder". I have written two functions that take a row (or entity) and swaps its sort order with the one nearest it (up or down, depending on which funct

  • Why can't I initialize a variable-sized array? 2011-07-09

    GCC gives no error when you initialize a variable-sized array as long as the variable is const, but when it isn't, it won't compile. What's the reason behind this? What's so wrong with doing: int size = 7; int test[size] = {3, 4, 5}; That won't compi

  • Why C++ cannot initialize a variable of type "derived class" with an rvalue of type "super class"? 2011-07-15

    Please consider the code below: class a { int a1; public: a() { printf("foo1\n"); } }; class b : public a { int a2; public: b() { printf("foo2\n"); } }; int main (int argc, const char * argv[]) { b *instance = new a(); return 0; } It gives Error: Can

  • Can you dynamically initialize multiple variables on one line in ruby? 2011-08-06

    (I've only been coding for a few weeks and this is my first question here, so please bear with me!) In ruby, I know that you can initialize multiple variables on a single line like this: a, b = 1, 2 However, I am wondering if it is possible to initia

  • Why we must initialize a variable before using it? 2011-10-16

    Possible Duplicate: What happens to a declared, uninitialized variable in C? Does it have a value? Now I'm reading Teach Yourself C in 21 Days. In chapter 3, there is a note like this: DON'T use a variable that hasn't been initialized. Results can be

  • How do I initialize the variable? 2011-10-28

    I'm writing a program for my class where I have to use a for loop to takes two numbers from the keyboard. The program should then raise the first number to the power of the second number. Use a for loop to do the calculation. I'm getting the error th

  • Initialize class variable just once - objective-C 2012-04-24

    So I've been trying to get a variable in my class (TrackViewController) initialize just once. This is done to keep the inserted id from the database known to the viewcontroller. In short: I have a TrackManager which receives a location update through

  • why we can't initialize static variable in constructor initialization list , but we can in constructor body 2015-01-11

    I always read initialization list is preferred over constructor body for variable initialization. I also know that static variable can be initialized outside the class when defiling them. But my question is why we can't initialize static variable in

  • Initialize final variable before constructor in Java 2009-03-24

    Is there a solution to use a final variable in a Java constructor? The problem is that if I initialize a final field like: private final String name = "a name"; then I cannot use it in the constructor. Java first runs the constructor and then the fie

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.165 (s). 11 q(s)