[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Improper handling of \! and \( while using Bash V4.1.2
From: |
Greg Wooledge |
Subject: |
Re: Improper handling of \! and \( while using Bash V4.1.2 |
Date: |
Mon, 6 Aug 2018 08:58:16 -0400 |
User-agent: |
NeoMutt/20170113 (1.7.2) |
On Mon, Aug 06, 2018 at 04:14:49PM +0530, anant garg wrote:
> $ mode=!
> $ [ "$mode" != "ro" -a "$mode" != "rw" ] && echo OK
> + '[' '!' '!=' ro -a '!' '!=' rw ']'
> bash: [: too many arguments
The use of -a and -o as conjunctions inside a test or [ command is to
be avoided. It's not POSIX compatible, and as you have now observed
first-hand, even in bash, it is perilous.
For this particular check, I would use case:
case $mode in
ro|rw) .... ;;
*) .... ;;
esac
But if for some reason you've got an if-boner, you could either use
two separate test commands:
if [ "$mode" != ro ] && [ "$mode" != rw ]
or use bash's [[ command:
if [[ $mode != ro && $mode != rw ]]
The former is POSIX comptaible. The latter is, of course, a bash extension.