Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-07-2006, 11:53 PM   #1
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...
for file in $(ls); do
    echo $file
...or not to be...
for file in `ls`; do
    echo $file
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.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-08-2006, 12:15 AM   #2
Registered: Jun 2004
Posts: 307

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
Registered: Oct 2005
Location: Rhode Island, USA
Distribution: Slackware, Xubuntu
Posts: 348

Rep: Reputation: 32
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:
bill@yogi:~$ test=apple
bill@yogi:~$ echo $test
bill@yogi:~$ echo $testjohn

bill@yogi:~$ echo ${test}john
bill@yogi:~$ echo $(( 1 + 1 ))
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.
2 members found this post helpful.
Old 10-08-2006, 12:37 AM   #4
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...
if test -z $var1 && test -x file; then
    echo blah
It makes you appreciate Bash alot.
Old 10-08-2006, 02:05 AM   #5
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

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


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 AM.

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