With bash, I see that using [[ ]] instead of [ ] with conditional
expressions will let you match a pattern on the right side:
myvar=aabbcc
if [ "$myvar" == *bb* ]; then
echo "we have a match"
fi
if [[ "$myvar" == *bb* ]]; then
echo "we have a match"
fi
This shows that only the second form, [[ ]], does a pattern match.
Question:
Are there other reasons to prefer [[ ]] over [ ] for use with
conditional expressions? And vice versa, are there reasons to prefer
[ ]?
2005-09-03, John Kelly wrote:
With bash, I see that using [[ ]] instead of [ ] with conditional
expressions will let you match a pattern on the right side:
myvar=aabbcc
if [ "$myvar" == *bb* ]; then
echo "we have a match"
fi
if [[ "$myvar" == *bb* ]]; then
echo "we have a match"
fi
--
This shows that only the second form, [[ ]], does a pattern match.
Question:
Are there other reasons to prefer [[ ]] over [ ] for use with
conditional expressions? And vice versa, are there reasons to prefer
[ ]?
I have never used the [[]] form, because it is not portable. If
you need pattern matching, you can use a case statement.
John Kelly wrote:
With bash, I see that using [[ ]] instead of [ ] with conditional
expressions will let you match a pattern on the right side:
--
myvar=aabbcc
if [ "$myvar" == *bb* ]; then
echo "we have a match"
fi
if [[ "$myvar" == *bb* ]]; then
echo "we have a match"
fi
I am not sure how this evaluates to true in your case:
[bmynars@bman:/home/bmynars]$
=[[ $myvar = *bb ]] && echo K || echo Failed
K
[bmynars@bman:/home/bmynars]$
=[[ $myvar = bb ]] && echo K || echo Failed
Failed
BTW, you do not have to use "quotes" when double square brackets are used
(another advantage of using double square brackets). Also, if Korn shell
and BASH shells are in question, the statement about "double square
brackets" not being portable is not quite true. [[ ]] will do mostly
what 'case' would. But again, we would get in the real of "esthetics" and
one's coding preferences.
--
This shows that only the second form, [[ ]], does a pattern match.
,[ ]
| Question:
`
Are there other reasons to prefer [[ ]] over [ ] for use with
conditional expressions? And vice versa, are there reasons to prefer
,[ ]
| [ ]?
`
2005-09-03, bmynars@gmail.com wrote:
John Kelly wrote:
>
>With bash, I see that using [[ ]] instead of [ ] with conditional
>expressions will let you match a pattern on the right side:
>>
>>
>myvar=aabbcc
>>
>if [ "$myvar" == *bb* ]; then
>echo "we have a match"
>fi
>>
>if [[ "$myvar" == *bb* ]]; then
>echo "we have a match"
>fi
>
I am not sure how this evaluates to true in your case:
[bmynars@bman:/home/bmynars]$
=[[ $myvar = *bb ]] && echo K || echo Failed
K
[bmynars@bman:/home/bmynars]$
=[[ $myvar = bb ]] && echo K || echo Failed
Failed
--
BTW, you do not have to use "quotes" when double square brackets are used
(another advantage of using double square brackets). Also, if Korn shell
and BASH shells are in question, the statement about "double square
brackets" not being portable is not quite true. [[ ]] will do mostly
what 'case' would. But again, we would get in the real of "esthetics" and
one's coding preferences.
For me it is not a questions of esthetics, but of standards. If
the double brackets were part of an accepted standard, I might
prefer them to a case statement in many instances; but they are
not.
bmynars@gmail.com writes:
>John Kelly wrote:
>
>if [[ "$myvar" == *bb* ]]; then
>echo "we have a match"
>fi
>
>I am not sure how this evaluates to true in your case:
>
[bmynars@bman:/home/bmynars]$
=[[ $myvar = *bb ]] && echo K || echo Failed
K
[bmynars@bman:/home/bmynars]$
=[[ $myvar = bb ]] && echo K || echo Failed
Failed
>
>
>BTW, you do not have to use "quotes" when double square brackets are used
>(another advantage of using double square brackets).
>
If I understand the Bolsky/Korn ksh book correctly, it's useful
to use double-quotes around a string in the right-hand part of
a comparison when it contains metacharacters but you want it
evaluated as a string rather than a pattern.
-Greg
John Kelly <jakelly@shtc.netwrote:
Are there other reasons to prefer [[ ]] over [ ] for use with
conditional expressions? And vice versa, are there reasons to prefer
man bash
man ksh
:-)
04.09.2005, John Kelly <jakelly@shtc.netwrote:
Sat, 3 Sep 2005 13:27:28 -0400, "Chris F.A. Johnson"
><cfajohnson@gmail.comwrote:
>
>I have never used the [[]] form, because it is not portable.
>
I needed a bash eqivalent of dirname, to be used for determining the
absolute pathname of the current script. For the "dirname" portion of
the bash code below, I could not think of anything better than using
[[ ]]
If anyone knows a better way
#v+
shell_dirname() {
while [ "x$1" != "x${1%/}" ]; do set -- "${1%/}"; done
case "$1" in
/* | "") set -- "${1%/*}"; [ -z "$1" ] && set / ;;
*) set "$PWD/${1%/*}" ;;
esac
echo "$1"
}
#v-
Fully SUS-compatible ($1 for echo always begins with "/"), entirely in
shell (unless echo is not a builtin).
I used something similar in my set of scripts for dealing with Slackware
packages ().
Copyright © 2008 www.emsdn.com • All rights reserved • CMS Theme by www.emsdn.com - 0.266