ksh/bash expr `cmd` vs $(cmd)

ksh/bash expr `cmd` vs $(cmd)

Post by <scott.walkins.. » Sat, 30 Dec 2000 14:38:54



Hi,

can anyone tell me if there is any practical difference in the way the shell
would handle the following, and if not why offer 2 options?

DATE=`date +%T`

DATE=$(date +%T)

cheers
Scott

 
 
 

ksh/bash expr `cmd` vs $(cmd)

Post by Derek M. Fly » Sat, 30 Dec 2000 06:48:17



> Hi,

> can anyone tell me if there is any practical difference in the way the shell
> would handle the following, and if not why offer 2 options?

> DATE=`date +%T`

> DATE=$(date +%T)

Your manual page should go into great detail on the differences between
the two.  Regardless, besides a few quoting rules with ` ` and more
leniancy on what can be in a $(...) (heredocs, etc).  There is no huge
difference.  If you use ksh, use $(...).  The ` ` is just there to let
you pull Bourne shell script into a Korn shell script and get reasonably
identical behavior.

 
 
 

ksh/bash expr `cmd` vs $(cmd)

Post by Barry Margoli » Sat, 30 Dec 2000 06:54:42





>> Hi,

>> can anyone tell me if there is any practical difference in the way the shell
>> would handle the following, and if not why offer 2 options?

>> DATE=`date +%T`

>> DATE=$(date +%T)

>Your manual page should go into great detail on the differences between
>the two.  Regardless, besides a few quoting rules with ` ` and more
>leniancy on what can be in a $(...) (heredocs, etc).  There is no huge
>difference.  If you use ksh, use $(...).  The ` ` is just there to let
>you pull Bourne shell script into a Korn shell script and get reasonably
>identical behavior.

The most significant difference is that $(...) nests easily, i.e. you can
write:

DATE=$(command1 $(command2))

To do this using backticks you have to escape them, and when you get nested
several levels deep you have to multiply the escapes, which is cumbersome.
I generally avoided it and used variables, e.g.

temp=`command2`
DATE=`command1 $temp`

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

1. ksh/bash: let cmd and test cmd interpret 08 (octal) differently

Beneath issue tested on Linux with bash and Solaris with ksh. I retrieve
current day using date +%d which always returns a two digits string. Which
means a leading zero for days 1 to 9. In (bash) shell this means interpret
number as an octal number.

day=`date +%d`
let day=${day}+1.
Suppose its day 8 (string is 08).  An error occurs --> let: day=08: value
too great for base (error token is "08").  Seems valid. Range for the octal
number is from 0 to 7.
Remark: let day=${day#0}+1. Is ok too. The string operator removes leading 0
before doing the addition.

No problems so far. Now my question. I can't explain behaviour of the
beneath code.

month="08"
if [ ${month} -eq 08 ]; then
    echo "match found"
fi
Running prog results in "match found".  Same for "if test  ${month} -eq 08"
or a if [ ${month} -eq 8 ];  (no leading zero).

I thought the 08 should be interpreted as an octal number (to be precise as
an illegal octal number) and create an error. It seems the shell
interpreteds number as DECIMAL here. Strange, can't explain....

Its no matter of life and death. It just bugs me that I can't figure it
out... Any help/pointers is very much appreciated as is an email on

Jan Bessels

2. mtools

3. HELP!: How do you get INN to take the news in from Suck?

4. backup CMD VS. savevg CMD

5. standard fdisk fails on redhat 5.1 setup

6. VAR=$(cmd) versus VAR=`cmd`

7. Desktop Pentium M Motherboard

8. capturing exit status for cmd when invoking 'xterm -e cmd'

9. how to assign out=`cmd` w/out waiting for "cmd" to finish??

10. is there a way to have var. assignments on cmd line affect only a builtin cmd?

11. disk space cmd for cmd line entries

12. How can I do xterm -e 'cmd > cmd.log' ?