|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Difference between if test and if [[ w.r.t shells
Hi,
I am trying to understand teh difference between the following if test $a -le 20 then print $a else print big fi and if [[ $a -le 20 ]]; then print $a else print big fi I put them in 2 different files named it aaa and bbb. Both of them gave same results when run in ksh or bash or csh. No errrors. May be it is because of the to first line I put in both the shell scripts which is #! /bin/ksh When I removed this line from aaa and bbb then also it was giving me same results except for one case bash does not recognize 'print' when there is no #! /bin/ksh at the top Any more thoughts on this Thanks |
|
#2
|
|||
|
|||
|
Short answer: put a line like
a="" at the top and one difference will become visible. This show the evolution of the shell. At first, if test $a -le 20 was all there was. And test was an external program sitting in /bin/test. The next step was to give test a second name called [. And there really was a /bin/[ to support it. The [ expected that it's last argument was ]. Some implementations of test would check for this, but usually they just ignore the last argument. So now syntax like: if [ $a -le 20 ] became possible. The next step was to make [ a shell built-in command. This standardized how it worked and made it much faster. But there is a problem: a="" if [ $a -le 20 ] Here $a will disappear before the test command sees its arguments. To the test command it looks like: if [ -le 20 ] which doesn't make sense. There is a solution, put double quotes around the $a like this "$a". When Dave Korn wrote the korn shell, he wanted to keep the [ syntax the same for compatability with existing scripts. But he wanted to solve the above problem. And he wanted to add a lot of stuff. So he invented the new if [[ ... ]] syntax. Bash and some other shells picked it up from ksh. |
|
#3
|
|||
|
|||
|
Nice post Perderabo. Very informative.
![]() |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Difference between if test and if [[ w.r.t shells |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|