|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|||
is there difference between two variations of sh command substitution?
PHP Code:
ref to "http://www.freebsd.org/cgi/man.cgi?query=sh&manpath=FreeBSD+8.1-RELEASE" command substitution section environment === FreeBSD abc.xyz.com 8.1-RELEASE FreeBSD 8.1-RELEASE #0: Mon Jul 19 02:55:53 UTC 2010 root@almeida.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC i386 |
|
|||
Thanks, rocket357.
The first form b=$( eval echo \$$_var ) which i understood is parsed like this: pass1: eval echo $aa ## \$ is treated as dollar sign $, $_var is aa pass2: eval it , and assign result (echo $aa 's output) to variable b is it right to think of it ? however, the second form b=`eval echo \\$$_var` which i can't read it. pass1: eval echo \$aa ## \\ -> \ (as manpage explained), $_var -> aa pass2: eval it ? echo \$aa -> $aa , $aa is output of echo , b=$aa, but the result is b=AAAA ($aa's val). how to understand the second form ? |
|
||||
Quote:
PHP Code:
|
|
||||
In the context of Bourne shell scripting:
For all practical intents and purposes, $(string) is just a synonym for `string`. $() was added as a more explicit way of doing `` around the 80s, so $() is not as portable as ``. I have not seen any shell that does not support $(), that is not old as petrified dog poo. I personally use `` in scripts because it gains a small bit of portability at no sacrifice in readability over $(). Yes, I'm to lazy to change from ``to $() in case I get stuck in an antique sh.
__________________
My Journal Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''. |
|
||||
Not sure how this relates to the OP's question (Which I find fairly confusing to be honest), but $() has an advantage in that it's better "nestable", for example consider:
Code:
$(ls | grep $(cat somefile)) As for portability: $() has long since been a part of the POSIX standard. Writing shell scripts with `` for portability is like modifying your programs for Windows 95.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
|
|||
I also prefer using $() wherever possible, apparently it was introduced with Korn shells and GNU's bash.
As mentioned by Carpetsmoker, this has been merged into POSIX/SUS standards and both are perfectly legal.. and a compliant 'sh' must support both. http://www.opengroup.org/onlinepubs/...l#tag_18_06_03 |
|
||||
CS, you'll just have to excuse those of us who test their scripts against shells who have yet to be certified as passing the standard, and prefer common-dialect portability over requiring the latest and greatest standards without real need of those features. Ditto for any C99 versus C89 debates. I'm a practical man not a theologian.
As to the OP, all I see is issues relating to $(), ``, and escaping, and wouldn't mind if the OP tried a more direct way of phrasing it.
__________________
My Journal Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''. |
|
||||
I use both $() and `` but seem to be shifting more toward $().
Advantages of `` are that it's easier to type and easier on the eyes, so I don't mind it in a short line such as % VAR=`command` but the individual back-quotes tend to get easily lost in longer statements, so there I'll use $() as it's easier to spot. But I also like consistency so maybe that's why the slide toward $() everywhere. |
|
||||
Quote:
|
|
|||
For entertainment, I looked up the standard to see what it says about the difference between backticks & $():
http://www.opengroup.org/onlinepubs/...xcu/chap2.html So here's the raw meat being thrown into the lions' cage: Code:
The $() form of command substitution solves a problem of inconsistent behaviour when using backquotes. For example: Command Output echo '\$x' \$x echo `echo '\$x'` $x echo $(echo '\$x') \$x Additionally, the backquoted syntax has historical restrictions on the contents of the embedded command. While the new $() form can process any kind of valid embedded script, the backquoted form cannot handle some valid scripts that include backquotes. For example, these otherwise valid embedded scripts do not work in the left column, but do work on the right: echo ` echo $( cat <<\eof cat <<\eof a here-doc with ` a here-doc with ) eof eof ` ) echo ` echo $( echo abc # a comment with ` echo abc # a comment with ) ` ) echo ` echo $( echo '`' echo ')' ` ) Because of these inconsistent behaviours, the backquoted variety of command substitution is not recommended for new applications that nest command substitutions or attempt to embed complex scripts. |
|
|||
Quote:
PHP Code:
The interpreter read in command line, and first two backslash is treated as \ literally, and \$ is treated as $ literally, $_var is replaced by $_var's value, "aa" after first interpreted, the interpreter also find it there's \$ because of result of first read, and \$ is treated as $ literally , is it right ot understand it ? ex2: the interpreter read in , and first two backslash is treated \ literally , and following two backslash is also, and following two dollar sign is replaced by current pid value. after first read(the first four backslash is interpreted \\), then this \\ is also interpreted \. if ex1 and ex2 do make sense. look at another example PHP Code:
the interpreter read in command, the first two backslash is \, the second pair is also \, the third pair is also \, and the following two dollar sign is shell pid after first read, the text is look like this: \\\1018_var, however, as you can see, the result is not expand $$ to pid, i think it interpret byte by byte, instead of read a line whole, like this: the first three pair backslash is interpreted three literal \, then \\\$$_var(assume it read byte by byte ) is interpreted as first two backslash is \ , third backslash and fourth dollar sign is literal $, and result of this is \$aa. question 1: how the interpreter interpret command line , line by line or byte by byte, or otherwise? question 2: why the \$ sequence not be interpreted, it's also in the list `$', ``', or `\'? |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Striking difference between the Linux community and BSD | windependence | Off-Topic | 79 | 6th December 2010 02:04 AM |
What's the difference between these two memory modules? | Carpetsmoker | General Hardware | 2 | 25th November 2008 03:13 PM |
difference between jdk16 and diablo 1.6 | cuongvt | FreeBSD Ports and Packages | 8 | 16th October 2008 12:17 AM |
difference between rc.conf and loader.conf | disappearedng | FreeBSD General | 5 | 3rd September 2008 05:54 AM |
Difference between chpass and pw | disappearedng | FreeBSD General | 9 | 7th July 2008 11:12 PM |