GNU bug report logs - #79901
Inconsistency between '-n STRING' and 'STRING' in TEST

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: coreutils; Reported by: Carlos Maiolino <cem@HIDDEN>; Keywords: notabug; Done: Pádraig Brady <P@HIDDEN>; Maintainer for coreutils is bug-coreutils@HIDDEN.
bug closed, send any further explanations to 79901 <at> debbugs.gnu.org and Carlos Maiolino <cem@HIDDEN> Request was from Pádraig Brady <P@HIDDEN> to control <at> debbugs.gnu.org. Full text available.
Added tag(s) notabug. Request was from Pádraig Brady <P@HIDDEN> to control <at> debbugs.gnu.org. Full text available.

Message received at 79901 <at> debbugs.gnu.org:


Received: (at 79901) by debbugs.gnu.org; 27 Nov 2025 15:15:56 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Nov 27 10:15:56 2025
Received: from localhost ([127.0.0.1]:37238 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1vOdit-0000Bn-QL
	for submit <at> debbugs.gnu.org; Thu, 27 Nov 2025 10:15:56 -0500
Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]:44222)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <pixelbeat@HIDDEN>)
 id 1vOdin-0000B2-0f
 for 79901 <at> debbugs.gnu.org; Thu, 27 Nov 2025 10:15:50 -0500
Received: by mail-wm1-x330.google.com with SMTP id
 5b1f17b1804b1-47796a837c7so6273335e9.0
 for <79901 <at> debbugs.gnu.org>; Thu, 27 Nov 2025 07:15:48 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1764256543; x=1764861343; darn=debbugs.gnu.org;
 h=content-transfer-encoding:in-reply-to:from:content-language
 :references:to:subject:user-agent:mime-version:date:message-id
 :sender:from:to:cc:subject:date:message-id:reply-to;
 bh=xdnz5+afg+JNYBX0TPe8DOVApfQaC2axt+bGg31Iu48=;
 b=kpEVw7ZEwzQYd46t+6xHYokUqjaN2IMmbx+/hbB6T3OPcLOv2Ahssrb46ty7ZmdCnx
 t2nPcK5QlwA+F+3pnaqfoojjHh53vNndP5AzzpvuGuX+vrpuWmukfkBGERYbwRYLty60
 QxklWiHQ1r4jEMcUlLGN/rItbZT6uA736LwA6nTgm6B1Q+CODYyyvS3mQ9d0+Hmd9YdX
 2Xz6e+/tydqspw950d+Ospv2o5sGdFHNDtbUIDYf+vK/IoF2iy+zks/P7qW8OfUDAqXR
 L6u3ivs+iOvkkqQIRmeq5WEsMMUAIe0b3+YR5SngmoAnS3u4jgMnYXKwLlm45AnCFdW1
 +XQA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1764256543; x=1764861343;
 h=content-transfer-encoding:in-reply-to:from:content-language
 :references:to:subject:user-agent:mime-version:date:message-id
 :sender:x-gm-gg:x-gm-message-state:from:to:cc:subject:date
 :message-id:reply-to;
 bh=xdnz5+afg+JNYBX0TPe8DOVApfQaC2axt+bGg31Iu48=;
 b=h0bscdWvl5v30C2IGnVtGKLh0ZELnpSl47fqLJRXqVj0X67EMYs2tTcj4CUTligwgD
 NT7IqBU5+gAuJf/F5VVRFrRqHTldyqAGnyzj9AQ41IOTdg2CbRxqrrpEbKla4+OrqP4p
 vPp3al9wD/jTcTNZ0CxSl0TTCAK0IoQCg3xqjTesYV1d6PFjokQ4rGmhIpqsCLbD8wx+
 mIK8x36BL/QTYony1/N4UD7EUaAntik6E5KCyyi3meyCrNLVy+h4C7fYjyyqvH+gWuaV
 pwf+Iw7ENJaMWx6VYqQWsdLKPmo//YD/+4wiN4UmLFwFn2xRXxL/pfKHTd2U9lR0gR7s
 NV5Q==
X-Forwarded-Encrypted: i=1;
 AJvYcCWWWjzJH/L+xtONtaFo+nr76qWetlSCSFYRuetpACl2vyV2wLtRvBqJc/x9xRO/AuWK6/dpmQ==@debbugs.gnu.org
X-Gm-Message-State: AOJu0Yw6uOt4S6XkJzaX8sk3dBuG386IbNxvUpN7fy84b810IjrW9qtG
 KBMcRc3OKAgpNHLdDw4VjQ51lhO344TeXaTW6DXI+zsNOOuAGP3BA397
X-Gm-Gg: ASbGncsKOJ/2j55CCJqzEJQHMx2pwTfWH14zY1rupKZmzapmdIzWDFRo7ef8NEHhAkv
 CDNiLJwQQ9i01OrqSfehkToa5sfFZAK6QsvqtY7UghixB45AWXSiKORxARaKNprjv3B+88TJtoP
 pFgUc1rja8LOnAQVyo1f59raI1ceFdyfEOEhAR6KYgRC0ngXb1Yi0WmwCIRUQ6lUohTCMXM1iQE
 zSMYd2fXA5DT/ZZhCwbCFcmlqCrguomKZXJSc2CKJkFcODWemDtrnpaKKx7zxnphnHwdBT949Vm
 fp8j+r8jG2aXtLtk4pXjUSzY1jBKLGyUtBQyxqhqEuK0ryjZ19rp/BAUT0P1QcjOUKYBcNztm4Y
 rWTsu3h+kd6/zDij9uRM0d58krqhKlTF4VDB1cTkREAFHqF9ktI8ot9lmDoN1S5MsYM7X0LO0HS
 swJMH0ahLsl7o2oSAmv0gexPgXZuXdBdRpcsJmOgPRNj1zIgvMMJLWKTHBNMcbDEK3V9oK3b9EG
 kG6oQ==
X-Google-Smtp-Source: AGHT+IG+gKPAql294ZszAkfOpAtM/LLytpKEZgHtT79WLr3Jd2Fda53XLsUDKE8vHKu3lHKGDjC/oQ==
X-Received: by 2002:a05:600c:4684:b0:477:9c73:2680 with SMTP id
 5b1f17b1804b1-477c01bf658mr234613745e9.23.1764256542420; 
 Thu, 27 Nov 2025 07:15:42 -0800 (PST)
Received: from [192.168.1.31]
 (86-44-211-146-dynamic.agg2.lod.rsl-rtd.eircom.net. [86.44.211.146])
 by smtp.googlemail.com with ESMTPSA id
 5b1f17b1804b1-4790add608bsm103509055e9.5.2025.11.27.07.15.41
 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
 Thu, 27 Nov 2025 07:15:41 -0800 (PST)
Message-ID: <8921d415-ccf4-46ee-8ef8-347b8a06d9ad@HIDDEN>
Date: Thu, 27 Nov 2025 15:15:39 +0000
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird Beta
Subject: Re: bug#79901: Inconsistency between '-n STRING' and 'STRING' in TEST
To: Carlos Maiolino <cem@HIDDEN>, 79901 <at> debbugs.gnu.org
References: <tzymi3atyshchzzutfn4jx7mevcp3sf4isxsfxuw77ajlclzrj@42qkand5ixgw>
Content-Language: en-US
From: =?UTF-8?Q?P=C3=A1draig_Brady?= <P@HIDDEN>
In-Reply-To: <tzymi3atyshchzzutfn4jx7mevcp3sf4isxsfxuw77ajlclzrj@42qkand5ixgw>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 79901
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

tag 79901 notabug
close 79901
stop

details below...

On 27/11/2025 12:33, Carlos Maiolino via GNU coreutils Bug Reports wrote:
> Hello,
> 
> I'm not sure if the behavior below is expected or perhaps a bug?
> I'm using GNU coreutils 9.6
> 
> According to the TEST(1) manpage:
> 
>         -n STRING
>                the length of STRING is nonzero
> 
>         STRING equivalent to -n STRING
> 
> 
> However, the small script below will produce inconsistent results, where
> only the first if conditional will evaluate to True:
> 
> #!/bin/bash
> 
> B=" "
> 
> if [ -n $B ]; then
>          echo "passed 1"
> fi
> 
> if [ $B ]; then
>          echo "passed 2"
> fi
> 
> 
> However it seems to me the variable expansion plays a role here, I'm not
> sure if the variable expansion was supposed to differ between "$B" and
> "-n $B" too, so perhaps it's a bash thing not a "test" thing?

You need to quote the variable expansion to maintain spaces,
otherwise `test -n $B` is equivalent to `test '-n'` which is true.

You want: [ -n "$B" ] && [ "$B" ] && echo both passed

cheers,
Padraig.




Information forwarded to bug-coreutils@HIDDEN:
bug#79901; Package coreutils. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 27 Nov 2025 14:43:03 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Nov 27 09:43:02 2025
Received: from localhost ([127.0.0.1]:36655 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1vOdD1-0006op-II
	for submit <at> debbugs.gnu.org; Thu, 27 Nov 2025 09:43:02 -0500
Received: from lists.gnu.org ([2001:470:142::17]:59706)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <cem@HIDDEN>) id 1vObBz-00075N-K7
 for submit <at> debbugs.gnu.org; Thu, 27 Nov 2025 07:33:48 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <cem@HIDDEN>) id 1vObBt-00010Q-4w
 for bug-coreutils@HIDDEN; Thu, 27 Nov 2025 07:33:41 -0500
Received: from tor.source.kernel.org ([172.105.4.254])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <cem@HIDDEN>) id 1vObBr-0007Y1-Na
 for bug-coreutils@HIDDEN; Thu, 27 Nov 2025 07:33:40 -0500
Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58])
 by tor.source.kernel.org (Postfix) with ESMTP id 864F260141
 for <bug-coreutils@HIDDEN>; Thu, 27 Nov 2025 12:33:10 +0000 (UTC)
Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4465C4CEF8
 for <bug-coreutils@HIDDEN>; Thu, 27 Nov 2025 12:33:09 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org;
 s=k20201202; t=1764246790;
 bh=TZbrqvzD+08DQNA8BXZfZgrgl9JO2+Sd41l3Aji1OTs=;
 h=Date:From:To:Subject:From;
 b=hlG7tHvosd2JxMPqxhBFeldGOyvZ4qRwA0aoSIs3DLCjkmzDX1Hnf2UFVihezMMVj
 G8BIL03+bV6HvLDWfAnHMrCks37JAqxl9NSDmy3RZSyn5t4++J1K7gymXDxUcwLIVt
 O9skNjkb+pVPk24l5Nk1f8+NgER7aI6WZL7ro6mySeTSx2bPvRfIwwHtVqKDKJyxFR
 McykcLZ8jMhNZaIqh1IQ5I19O+pz5xG4U8zgxqdX0Xd6qSR6ptA4jhSbMqyn1f7tlA
 oGaX+rs5eVYp0i/Qyu4b9VxA4Wgxx/FkE5inyELjISCQtsKO5jZQN3udBZnX6xIz5s
 yUXRseWCb4aIw==
Date: Thu, 27 Nov 2025 13:33:06 +0100
From: Carlos Maiolino <cem@HIDDEN>
To: bug-coreutils@HIDDEN
Subject: Inconsistency between '-n STRING' and 'STRING' in TEST
Message-ID: <tzymi3atyshchzzutfn4jx7mevcp3sf4isxsfxuw77ajlclzrj@42qkand5ixgw>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Received-SPF: pass client-ip=172.105.4.254; envelope-from=cem@HIDDEN;
 helo=tor.source.kernel.org
X-Spam_score_int: -22
X-Spam_score: -2.3
X-Spam_bar: --
X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.224,
 DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001,
 SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 1.0 (+)
X-Debbugs-Envelope-To: submit
X-Mailman-Approved-At: Thu, 27 Nov 2025 09:42:58 -0500
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.0 (/)

Hello,

I'm not sure if the behavior below is expected or perhaps a bug?
I'm using GNU coreutils 9.6

According to the TEST(1) manpage:

       -n STRING
              the length of STRING is nonzero

       STRING equivalent to -n STRING


However, the small script below will produce inconsistent results, where
only the first if conditional will evaluate to True:

#!/bin/bash

B=" "

if [ -n $B ]; then
        echo "passed 1"
fi

if [ $B ]; then
        echo "passed 2"
fi


However it seems to me the variable expansion plays a role here, I'm not
sure if the variable expansion was supposed to differ between "$B" and
"-n $B" too, so perhaps it's a bash thing not a "test" thing?

Cheers,
Carlos




Acknowledgement sent to Carlos Maiolino <cem@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-coreutils@HIDDEN. Full text available.
Report forwarded to bug-coreutils@HIDDEN:
bug#79901; Package coreutils. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Thu, 27 Nov 2025 15:30:01 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.