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)
COMMAND2=$(blah)

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`
COMMAND2=`blah`

echo -n "$COMMAND"; echo -n "$COMMAND2"
lolaferrariAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

$(command)

or like this using backticks:

`command`
0
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
.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)"
.bashrc08:13:49$ 

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

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

There is a great difference between
printf %s "COMMAND"; printf %s "COMMAND2"
and
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"
and
echo -n "COMMAND"; echo -n "COMMAND2"
or between
printf %s "$COMMAND"; printf %s "$COMMAND2"
and
echo -n "$COMMAND"; echo -n "$COMMAND2"
0
Duncan RoeSoftware DeveloperCommented:
@gheist: $( comes from ksh
0
farzanjCommented:
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
http://mywiki.wooledge.org/BashPitfalls


This link explains differences in detail.
http://mywiki.wooledge.org/BashFAQ/082
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.