LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-07-2006, 11:53 PM   #1
unreal128
Member
 
Registered: Jun 2003
Distribution: SuSE, Slackware, Gentoo
Posts: 207

Rep: Reputation: 30
Bash: When to use backtick and when to use $().


I read in some documentation and bash script comments that it is proper to use $() for program output rather than the backticks ``. For example, to be...
Code:
for file in $(ls); do
    echo $file
done
...or not to be...
Code:
for file in `ls`; do
    echo $file
done
That is the question.

I know you should usually just put an asterisk there but I am just using it as an example. Any comments or ideas? Are they both useful but should be used appropiately? If so, what is the appropiate use of each.
 
Old 10-08-2006, 12:15 AM   #2
cjcox
Member
 
Registered: Jun 2004
Posts: 305

Rep: Reputation: 42
Use backtick when using Bourne Shell (e.g. Solaris's shell for the root user).

The $(cmd) notation is better since it as a definite begin and end. This makes nesting commands easier (you'd have to do escaping if you used backticks).

If your shell (e.g. bash) supports using $(cmd), then use it. If you are worried about portability and want to make sure your scripts will run on the old Bourne Shell... use backticks.
 
Old 10-08-2006, 12:16 AM   #3
zetabill
Member
 
Registered: Oct 2005
Location: Rhode Island, USA
Distribution: Slackware, Xubuntu
Posts: 348

Rep: Reputation: 30
A Linux professor in college told us that the grave (`) was the original syntax but was far too easily confused with the single-quote ('). He told us that while we won't lose points on our assignments for using the grave, he would almost always make a mistake and take points off because he thought that we were using a single quote. This was on quizzes when we had to handwrite little snips of code and such. His retort to our protests was almost always telling us to use the newer syntax... $(). This was for variables, as well, for disambiguation. He also said that it's easier to look at code and think, in your mind, that anything that starts with $ means it's representing an alternate value.

For example:
Code:
bill@yogi:~$ test=apple
bill@yogi:~$ echo $test
apple
bill@yogi:~$ echo $testjohn

bill@yogi:~$ echo ${test}john
applejohn
bill@yogi:~$ echo $(( 1 + 1 ))
2
bill@yogi:~$ echo $(date)
Sun Oct 8 01:07:52 EDT 2006
You can see that variables are ${variable}, math expressions are $((expression)), and command substitution is $(command).

So stick with that, and anyone that looks at your code will know that anything that starts with $ will be something other than what is in its brackets... and the brackets tell you what it is.

However you can still get away with graves, expr, and regular variables. And it's always good to know how they're used if you find yourself looking at other people's work.

I hope I helped you.

EDIT: I forgot about the old Bourne shells, cjcox. There was in fact a time where $ was only for variables. I didn't realize that the regular Bourne shell was still in use. Always learning....

Last edited by zetabill; 10-08-2006 at 12:20 AM.
 
Old 10-08-2006, 12:37 AM   #4
unreal128
Member
 
Registered: Jun 2003
Distribution: SuSE, Slackware, Gentoo
Posts: 207

Original Poster
Rep: Reputation: 30
Thanks to both of you for your responses. I will stick to the $() notation as a one-way in my bash scripting. It makes sense to me now about the backticks; I just recently moved to using $() instead.

Before this I used to script in Bourne on SCO OpenServer (ugh.) Now that I am using Bash on Linux, I have alot more possibilities but still have to shed off some old scripting habits from Bourne.

It was nice though to learn how to write a pretty universal script that could be run on any Unix/Linux system since Bourne has been around for awhile, like such...
Code:
if test -z $var1 && test -x file; then
    echo blah
fi
It makes you appreciate Bash alot.
 
Old 10-08-2006, 02:05 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
See this thread for a similar discussion:

http://www.linuxquestions.org/questi...d.php?t=458080
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Sleep vs crontab and bash serial port nutthick Programming 4 06-01-2006 02:42 AM
(bash) echo "#!/bin/bash" event not found - trying to generate profiles automatically jimieee Programming 9 05-03-2006 10:24 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
$LINENO can't be modified in bash 3.0, while it can be in bash 2.05b Darwish Linux - Software 1 11-07-2005 02:57 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM


All times are GMT -5. The time now is 02:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration