LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-12-2016, 05:20 PM   #1
penguinbody
LQ Newbie
 
Registered: Jan 2016
Posts: 10

Rep: Reputation: Disabled
Post Command Substitution: $(command) vs `command`


The LPI Certification In a Nutshell says:

"Another form of command substitution is `command`. The result is the same, except that the back quote (or backtick) syntax has some special rules..."

I followed the reference to the BASH manual for an explanation of the special rules here.

Does that mean the characters after the first \ here can be interpreted into something else as opposed to the literal meaning? Like what? I need some examples for that to be clearer.

>`\$;command`
>`\`;command`
>`\\`;command`

Lastly, what does this statement mean: The first backquote not preceded by a backslash terminates the command substitution.
 
Old 04-12-2016, 06:14 PM   #2
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Your context seems missing a bit. Assuming something is preceding the ">" and it's not just a command prompt symbol. But `` and $() are interchange-able. Except that $() allows for the nesting of commands. The \ char is an escape character. And the ; char is a line separator. With special chars like $ ^ ? and stuff. The non-special characters just get passed as an uninterpreted character less the preceding \.
 
Old 04-13-2016, 01:27 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by penguinbody View Post
Another form of command substitution is `command`.
i heard people say that this is "the old way", only kept for backward compatibility or some such.
 
Old 04-13-2016, 01:52 AM   #4
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,680
Blog Entries: 19

Rep: Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492
I've noticed that the LFS books, when they need to nest two command substitutions, use backquotes for one and the $() construct for the other. They don't nest $() constructs. I suppose this is for reasons of legibility and comprehensibility.

Backquotes may be "the old way" but a lot of us still use them because it's that's the way we learned to do it.
 
Old 04-13-2016, 03:21 AM   #5
cnamejj
Member
 
Registered: Mar 2015
Distribution: Ubuntu
Posts: 37

Rep: Reputation: Disabled
I think backticks (meaning ``) for getting the results of a command should be consider "legacy baggage" at this point. And I always use the $() construct instead, even for nested calls. It's so much cleaner and easier to read. And there's no confusion about whether a backtick is opening a command or closing one.
 
1 members found this post helpful.
Old 04-13-2016, 04:10 AM   #6
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,167

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Example:
#pwd
/usr/my_pics


Current print working directory is /usr/my_pics

#cp `pwd` /home

or

#cp $(pwd) /home


cp $(pwd) /home or cp `pwd` /home
is equivalent to:
cp /usr/my_pics /home


which copies my_pics folder to /home folder.

Backtick runs the contents of the enclosed string, and used it as a parameter to the other command on the line, in this case the "cp" command.

I think to save bytes use backtick, for clarity purposes use $().
 
1 members found this post helpful.
Old 04-13-2016, 05:49 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
cp -rv /usr/my_pics ~/
Would work too. The -rv is just in case directories are present within the working directory, and output to term to see what's going on. If /home is actually meant to mean/home/user. In real world situations. What logical reasons would one have to put files in just the /home directory minus a user directory or subdirectory?

Quote:
>`\$;command`
>`\`;command`
>`\\`;command`

Lastly, what does this statement mean: The first backquote not preceded by a backslash terminates the command substitution.
to eliminate the termination just add another ` because they have to have an opening and closing, or matching pairs. I think they where just giving you a "matter of fact".

Last edited by BW-userx; 04-13-2016 at 06:31 AM.
 
Old 04-13-2016, 11:19 AM   #8
Weapon S
Member
 
Registered: May 2011
Location: Netherlands
Distribution: Debian, Archlinux
Posts: 262
Blog Entries: 2

Rep: Reputation: 49
Quote:
Originally Posted by penguinbody View Post
The LPI Certification In a Nutshell says:
Lastly, what does this statement mean: The first backquote not preceded by a backslash terminates the command substitution.
That's just an escape sequence. A backslash "escapes" the character. If you want to run a command containing a backtick (in this case) you will have to tell Bash to not interpret that backtick as the ending of command substitution clause. So you make the backtick literal with a backslash, making it lose its special meaning of ending your clause.

http://wiki.bash-hackers.org/syntax/expansion/cmdsubst
 
Old 04-14-2016, 01:29 AM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by JJJCR View Post
Example:
#pwd
/usr/my_pics


Current print working directory is /usr/my_pics

#cp `pwd` /home

or

#cp $(pwd) /home


cp $(pwd) /home or cp `pwd` /home
is equivalent to:
cp /usr/my_pics /home


which copies my_pics folder to /home folder.

Backtick runs the contents of the enclosed string, and used it as a parameter to the other command on the line, in this case the "cp" command.

I think to save bytes use backtick, for clarity purposes use $().
that's not a good example.
it does not save any bytes either way.
also it will not work like this (cp throws a warning and doesn't copy anything, at least on my version).
i'd do it like this:
Code:
cp -r . /home
but you're likely to not have permission to copy anything to /home, i guess it really should be
Code:
cp -r . "$HOME"
and to "save bytes":
Code:
cp -r . ~
also, you usually don't need a command to get the present working directory, there's an env.variable called "$PWD", so this is equivalent:
Code:
cp -r "$PWD" "$HOME"
 
1 members found this post helpful.
Old 04-15-2016, 07:00 AM   #10
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Backtics are for portability. Even now, not all systems use bash unless the admins have installed it. Many have ksh/csh/sh (The Bourne shell) which don't use the $() construct.

A few of us even think bash is a bit bloated with things that shouldn't be there...
 
1 members found this post helpful.
  


Reply



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 - command substitution kopert Programming 8 09-18-2012 04:13 AM
command substitution in linux anu_123 Linux - Newbie 3 06-17-2010 12:31 AM
Bash Command Substitution dakensta Programming 5 11-30-2006 03:10 PM
Command substitution and sed daYz Linux - General 9 11-04-2006 01:15 AM
command substitution: ^ rhxk Linux - General 2 04-06-2006 09:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:51 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration