Size: 3785
Comment: fixed typo
|
Size: 3856
Comment: some clean-up; one example was reversed, and table syntax changed slightly in moin 1.5 apparently
|
Deletions are marked like this. | Additions are marked like this. |
Line 11: | Line 11: |
if [ -f "$filename" ] | if [ ! -f "$filename" ] |
Line 31: | Line 31: |
To cut a long story short: {{{[}}} implements the old, portable syntax of the command. Although all modern shells have built-in implementations, there usually still is an external executable of that name, e.g. {{{/bin/[}}}. {{{[[}}} is a new improved version of it, which is a keyword, not a program. This has beneficial effects on the ease of use, see below. {{{[[}}} is understood by KornShell, ["BASH"] (e.g. 2.03), KornShell93, ["POSIX"] shell, but not by the older BourneShell. | To cut a long story short: {{{[}}} implements the old, portable syntax of the command. Although all modern shells have built-in implementations, there usually still is an external executable of that name, e.g. {{{/bin/[}}}. {{{[[}}} is a new improved version of it, which is a keyword, not a program. This has beneficial effects on the ease of use, see below. {{{[[}}} is understood by KornShell, ["BASH"] (e.g. 2.03), KornShell93, and the ["POSIX"] shell, but not by the older BourneShell. |
Line 36: | Line 36: |
||<rowspan="4">string comparison||>||(not available)||-|| | ||<|4>string comparison||>||(not available)||-|| |
Line 40: | Line 40: |
||<rowspan="2">expression grouping||&&||-a||{{{[[ -n $var && -f $var ]] && echo "$var is a file"}}}|| | ||<|2>expression grouping||&&||-a||{{{[[ -n $var && -f $var ]] && echo "$var is a file"}}}|| |
Line 54: | Line 54: |
* No field splitting will be done for {{{[[}}} (and therefore many arguments need not to be quoted) | * No field splitting will be done for {{{[[}}} (and therefore many arguments need not be quoted) |
Line 66: | Line 66: |
This makes {{{[[}}} easier to use and less error prone. | This makes {{{[[}}} easier to use and less error-prone. |
Line 68: | Line 68: |
* No file name generation will be done for {{{[[}}}. Therefore the following line tries to match the contents of the variable $path with the pattern {{{/*}}} | * No file name generation will be done for {{{[[}}}. Therefore the following line tries to match the contents of the variable {{{$path}}} with the pattern {{{/*}}} |
Line 78: | Line 78: |
{{{[[}}} is strictly used for strings and files. If you want to compare numbers, use ArithmethicExpression ((''expression'')), e.g. | (If you need to do that using Bourne shells, use {{{case}}} instead.) * {{{[[}}} is strictly used for strings and files. If you want to compare numbers, use ArithmethicExpression ((''expression'')), e.g. |
What is the difference between the old and new test commands ([ and [[)?
[ ("test" command) and [[ ("new test" command) are both used to evaluate expressions. Some examples:
if [ -z "$variable" ] then echo "variable is empty!" fi if [ ! -f "$filename" ] then echo "not a valid, existing file name: $filename" fi
and
if [[ -e $file ]] then echo "directory entry does not exist: $file" fi if [[ $file0 -nt $file1 ]] then echo "file $file0 is newer than $file1" fi
To cut a long story short: [ implements the old, portable syntax of the command. Although all modern shells have built-in implementations, there usually still is an external executable of that name, e.g. /bin/[. [[ is a new improved version of it, which is a keyword, not a program. This has beneficial effects on the ease of use, see below. [[ is understood by KornShell, ["BASH"] (e.g. 2.03), KornShell93, and the ["POSIX"] shell, but not by the older BourneShell.
Although [ and [[ have much in common, and share many expression operators like "-f", "-s", "-n", "-z", there are some notable differences. Here is a comparison list:
Feature |
new test [[ |
old test [ |
Example |
string comparison |
> |
(not available) |
- |
< |
(not available) |
- |
|
== (or =) |
= |
- |
|
!= |
!= |
- |
|
expression grouping |
&& |
-a |
[[ -n $var && -f $var ]] && echo "$var is a file" |
|| |
-o |
- |
|
Pattern matching |
== (or =) |
(not available) |
[[ $name = a* ]] || echo "name does not start with an 'a': $name" |
In-process regular expression matching |
=~ |
(not available) |
[[ $(date) =~ ^Fri\ ...\ 13 ]] && echo "It's Friday the 13th!" |
Special primitives that [[ is defined to have, but [ may be lacking (depending on the implementation):
Description |
Primitive |
Example |
entry (file or directory) exists |
-e |
[[ -e $config ]] && echo "config file exists: $config" |
file is newer/older than other file |
-nt / -ot |
[[ $file0 -nt $file1 ]] && echo "$file0 is newer than $file1" |
two files are the same |
-ef |
[[ $input -ef $output ]] && { echo "will not overwrite input file: $input"; exit 1; } |
negation |
! |
- |
But there are more subtle differences.
No field splitting will be done for [[ (and therefore many arguments need not be quoted)
file="file name" [[ -f $file ]] && echo "$file is a file"
will work even though $file is not quoted and contains whitespace. With [ the variable needs to be quoted:
file="file name" [ -f "$file" ] && echo "$file is a file"
This makes [[ easier to use and less error-prone.
No file name generation will be done for [[. Therefore the following line tries to match the contents of the variable $path with the pattern /*
[[ $path = /* ]] && echo "\$path starts with a forward slash /: $path"
The next command most likely will result in an error, because /* is subject to file name generation:
[ $path = /* ] && echo "this does not work"
(If you need to do that using Bourne shells, use case instead.)
[[ is strictly used for strings and files. If you want to compare numbers, use ArithmethicExpression ((expression)), e.g.
i=0 while ((i<10)) do echo $i ((i=$i+1)) done
When should the new test command [[ be used, and when the old one [? If portability to the BourneShell is a concern, the old syntax should be used. If on the other hand the script requires ["BASH"] or KornShell, the new syntax could be preferable.