Re: What's the disadvantage for script if [ $x = 'WORLD ];



On Mar 22, 3:05 am, Ben Finney <ben+u...@xxxxxxxxxxxxxxx> wrote:
Miao Jiang <jiangfri...@xxxxxxxxx> writes:
Hello,
for script
x='HELLO'
if [ $x = 'HELLO' ];
some people write it in
if [ "$x" = 'HELLO' ];

Is there any disadvantage if write it in [ $x = 'HELLO' ] ?

Failing to quote variable expansions is a common mistake:

    $ x="HELLO"
    $ if [ $x = "HELLO" ] ; then printf "Equal\n" ; else printf "Unequal\n" ; fi
    Equal

    $ x="WORLD"
    $ if [ $x = "HELLO" ] ; then printf "Equal\n" ; else printf "Unequal\n" ; fi
    Unequal

    $ x=""
    $ if [ $x = "HELLO" ] ; then printf "Equal\n" ; else printf "Unequal\n" ; fi
    bash: [: =: unary operator expected
    Unequal

    $ x="HELLO WORLD"
    $ if [ $x = "HELLO" ] ; then printf "Equal\n" ; else printf "Unequal\n" ; fi
    bash: [: too many arguments
    Unequal

    $ x="! -z"
    $ if [ $x = "HELLO" ] ; then printf "Equal\n" ; else printf "Unequal\n" ; fi
    Equal

If the variable can't be guaranteed to be a single word every time – if
it might be empty, or might contain characters special to the shell, or
might simply have a value not under your complete control – quote it
when expanding.

In general: quote any variable expansion unless you have a good reason
not to.

--
 \       “Science shows that belief in God is not only obsolete.. It is |
  `\                        also incoherent.” —Victor J. Stenger, 2001 |
_o__)                                                                  |
Ben Finney

Thank you. double quote is quite necessary, especially do something
with strings.
and I write a script to understand it more
test.sh
#!/bin/sh

for arg; do
echo $arg
done

x="! -z"
../test.sh $x = "HELLO"
output:
!
-z
=
HELLO

../test.sh "$x" = "HELLO"
output:
! -z
=
HELLO
.



Relevant Pages

  • Re: Did Capa say "if thats chess, you can keep it"? (was: Kasparov V Karpov)
    ...   It also appears to be information no one else has. ... have read that Capablanca said. ... own quote about Pandolfini, since it doesn't exist. ... table showing that Lasker finished last at Nottingham 1936. ...
    (rec.games.chess.misc)
  • Re: Stein: Cells might be intelligent
    ...   Quote ... in the cell itself so that's probably a big difference between the two ... Darwinists have never observed natural species being originated ... ...
    (talk.origins)
  • Re: COMP.DSP 2010 pictures and presentations
    ... am I glad I wore a clean shirt ...    excitement I kind of lost track myself. ... do you know from where the above quote came? ... Sadly, if you had a Billy Beer can, it probably would not be ...
    (comp.dsp)
  • Re: Kindle fans: People are starting to notice the DRM crap
    ... Are you being paid by Amazon to try to defuse the ...    same time. ... someone a liar for posting a quote from the URL you provided. ... Some of us who have been posting for a while (my first usenet post was ...
    (rec.arts.sf.written)
  • Re: Why Does John Hillery Attack Sam Sloan All the Time?
    ... another quote (I am trying to avoid giving enough information to ...   Presumably this refers to Hillery's blog on the Wester Chess ... the USCF with his weekly paranoid fantasies. ... to cast a protest vote, ...
    (rec.games.chess.politics)