Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Broken backquote behavior in bash?



 On Fri, Jul 25, 2008 at 07:39:49AM -0400, Jerry Feldman wrote: 
> Just one comment, the preferred Bash construct is to use $() rather 
> than backquotes. 
> $(/usr/bin/id -u) 

I've recently seen discussions about this on this list or elsewhere, 
and I can't really agree.  I've seen documentation that refers to 
backticks as deprecated, but honestly the idea seems really silly to 
me.  Backticks work, are more portable,  and are necessary for 
compatibility with all versions of the Bourne and Korn shells.  I 
think POSIX specifies them too (but may be mistaken), and that usage 
is common in other languages too, so I doubt they'll be going away any 
time soon...  Backticks are also still very widely used in all manner 
of shell scripts that come with Linux systems these days... 

It is worth noting, however, that you can't nest backticks, but you 
can nest $() in bash.   

  $ echo -e "4\c" > foo 
  $ echo $(expr $(cat foo) + 6) 
  10 

You can also nest one inside the other. 

  $ echo $(expr `cat foo` + 6) 
  10 

Anyway... 

> > I've got a server that's been giving strange errors lately. Most 
> > noticeably, when I login, 
> > I get several errors of the form 
> > 
> >     -bash: [: =: unary operator expected 

What output do you get when you run id -u on the command line?  If you 
get none, then this test will fail with the error you provided. 

> > I've traced these to files under /etc/profile.d, and on further 
> > testing I find that the offending lines are using backquotes, e.g. 
> > 
> >         if [ `/usr/bin/id -u` = 0 ] ; then 

FWIW, I usually try to avoid this kind of construct for exactly this 
reason (though at least it uses '=' instead of '-eq').  [I realize 
this is probably not your shell script.]  This is slightly more 
reliable: 

  id=`/usr/bin/id -u` 
  if [ "$id" = "0" ]; then 
  ... 

The reason to do it this way is because if $id is empty, the test will 
compare an empty string (enforced by the quotes).  You could also do 

  [ "`/usr/bin/id -u`" = "0" ] 

which is the same thing.  Using a numeric comparison can break with 
variables or expressions which can evaluate to nothing (e.g. a command 
substitution that failed because of an error), because the arguments 
must be numeric, and you can not compare an empty string numerically. 

> > When I try to use backquotes on the command line on this server, I 
> > get no output.  Even stranger, if I have a suspended vi job, then 
> > running something in backquotes 
> > terminates the vi process: 
> > 
> >     $ vi foo 
> >     ^Z 
> >     [1]+  Stopped                 nvi foo 
> >     $ echo `echo bar` 
> > 
> >     [1]+  Terminated              nvi foo 


BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org