Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm busy writting shell script (using Bash) where I need to populate some variable from the returned result of other command.

So I'm using a syntax like this one :

VAR=`command 2>/dev/null`

But when looking for information on script shells, I found often that the above can be achieved by using the following syntax too :

VAR=$(command 2>/dev/null)

Both are working for me (I have received the expected result in both case) and I'm thus wondering if I can still use both form interchangeably or if I have to stick to one form to be sure to stay compliant with the future evolution of shell ?

share|improve this question
    
    
The links by belacqua offer a lot of information about the differences and usage. Closing since it is not related to an Ubuntu issue, but more of a programming one. I highly recommended following the links mentioned above. –  Luis Alvarado Jul 24 at 17:12

2 Answers 2

Use dollar. Backticks are semi-deprecated, because they are more complicated to use (see the link), and there are no advantages to them unless you're doing code golf and absolutely need to save a single character. They probably won't be removed from popular shells anytime soon though, so you're safe using either for now.

share|improve this answer

Stick with the dollar sign notation $() whenever you can. Backticks get cumbersome/confusing when you start needing to do nested quoting. Ex:

$ FOO=`echo "foo's"`
$ echo $FOO
foo's

Replacing the backticks with $() yields the same output, and is easier to read (fewer quotes of varrying angles to discern):

$ FOO=$(echo "foo's")
$ echo $FOO
foo's

That, and each quotation mark (single quote, double quote, backtick) means something different in the shell, so using $ can help remove a level of complexity. And the developer who follows you will thank you, for he/she will have an easier time figuring out what you did.

share|improve this answer
    
Backticks cannot even be nested functionally. –  Bernhard Jun 24 at 18:29
    
@Bernhard My example shows what I had in-mind with that statement. –  BryceAtNetwork23 Jun 24 at 18:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.