• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

difference between ` ` and $( )

I have commands that I want to output on the same line without newline characters.

To get this working I had to use this syntax

COMMAND=$(blah bah)

printf %s "COMMAND"; printf %s "COMMAND2"

I tried the below syntax first but this didn't work. What's the difference between `  ` and $( ) and why did echo -n not work ?

COMMAND=`blah bah`

echo -n "$COMMAND"; echo -n "$COMMAND2"
4 Solutions
lolaferrariAuthor Commented:
Thanks for that. It mentions command substitution can be done both ways below. Is there any differences? I still don't understand why echo -n "$COMMAND" ; echo -n "$COMMAND2" output the results with the newline when I thought -n was supposed to remove the new line.


or like this using backticks:

Duncan RoeSoftware DeveloperCommented:
Did you really enter echo -n "$COMMAND" ; echo -n "$COMMAND2"? I would have expected that you would enter echo -n "$($COMMAND)" ; echo -n "$($COMMAND2)" (i.e. with parentheses). If you did that, echo would not append a newline to the output from COMMAND or COMMAND2
07:43:12$ ls .bashrc
08:12:02$ COMMAND='ls .bashrc'
08:12:28$ echo $COMMAND
ls .bashrc
08:12:57$ echo -n "$COMMAND"
ls .bashrc08:13:14$ echo -n "$($COMMAND)"

Open in new window

If you got newlines, your command must have explicitly output them.

Functional difference between $(command) and `command` : none. Syntactically though, the newer $(command) form can be nested but back ticks can not. Nesting can dramatically shorten scripts, by obviating the need to keep intermediate results. Absent nesting, some people find one variant easier to read than the other. Or easier to type.
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

$( is non-portable bash-ism, otherwise they act the same....
Assuming blah blah does not contain $( ) or ` `
and your shell is not something like csh
there should be no difference between
COMMAND=$(blah bah)
COMMAND=`blah bah`

There is a great difference between
printf %s "COMMAND"; printf %s "COMMAND2"
echo -n "$COMMAND"; echo -n "$COMMAND2"
(Unless blah blah happened to be equivalent to echo COMMAND)

There should be no difference between
printf %s "COMMAND"; printf %s "COMMAND2"
echo -n "COMMAND"; echo -n "COMMAND2"
or between
printf %s "$COMMAND"; printf %s "$COMMAND2"
echo -n "$COMMAND"; echo -n "$COMMAND2"
Duncan RoeSoftware DeveloperCommented:
@gheist: $( comes from ksh
Basically they try to accomplish the same thing, but they are different. There are many differences.  For example nested quotations work with $() but not with ``

See #5 in link

This link explains differences in detail.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now