Path: csiph.com!goblin3!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: Peng Yu Newsgroups: gnu.bash.bug Subject: Re: Should [[ -v 1 ]] be supported? Date: Thu, 27 Dec 2018 18:39:26 -0600 Lines: 44 Approved: bug-bash@gnu.org Message-ID: References: <5dac2cf2-2fac-0fd1-058f-6a84a3271738@case.edu> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" X-Trace: usenet.stanford.edu 1545958196 11039 208.118.235.17 (28 Dec 2018 00:49:56 GMT) X-Complaints-To: action@cs.stanford.edu Cc: bug-bash To: Martijn Dekker Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=8TKXj+VOCO4iPQybVk/GHnM/NC/TYfbcioPvLNCvilw=; b=J4QnHYxsxtiPJXwg6jD5B/plLireZ6QufJrRzjkQS3bei42xlJwwIUfooi2+Wjd7xa Zf7f2tgecE/H51Ap2JlD3yPx/Qlt+JikUjrKFsqJRtCWu5CY7k0KOZYCRSkWk7dADuJ5 aw7lhg2ZqZRyDDRLPrPTrq54zxDFHewZ/daa7TYryWxmbt+z9p3FThS5yatSHL79jjQE GGKIia+vv5qV4T1s+qKz5E79/jC4SXLvBhDJbPbMxQLo8fB8XcLLW1v74aPf5Xn5nrLA qwoI4TdnSMFJL3CFyRahZDmxA90Kwl9thPC4oayBa0PRiYWVVU0DXhQs3/uJqa9WWN/X 8fmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=8TKXj+VOCO4iPQybVk/GHnM/NC/TYfbcioPvLNCvilw=; b=j3N75HURp6OChAE23C6y7cCFeW18qdMQq636eDzbqKo+Qex2jCrCbPXz9cmckSiiFZ bJmH28y+3JF3vIYVe2txL9LUiNYcuQs/xr5oCzTz4EfrgKEcJzEdmlNklbV4deZBDpg3 fdLRckge+9rzPPzB7+RfXjhve18S15mvFAbVYTitYk/Y1qHqzv/8NGGQkbQIRym4snKU 7MaC/3g8RIGpittG6iNmYENiOWQvlxnb/ORctLiIpBjYjoid0jN9AHxh/n7KfaWuDr4S axB4UTv9rUOMWfn5+Mgm9zLhhXFGNxEOaOcR5njeGqEzd37THJHj8GAe747WKiA5Y6QC me+w== X-Gm-Message-State: AJcUukev8rXreTLxbyQc8jg1DE+mW2Iz0b9bgFbz1fetnggxeM5lJ3Vq WB19ZerWsAITWZtQgsVuLYf45m/+fuV9q+JuYWev4fYg X-Google-Smtp-Source: AFSGD/VhtcGSf/fotml9g0kgdtIQQSiVDNDlmCXJ6+1mtr7WLSmpUWHhgtlFdRgW/BsmYP7cVrt6xeLrYqp7ecGvxOA= X-Received: by 2002:a24:6553:: with SMTP id u80mr15573850itb.0.1545957577679; Thu, 27 Dec 2018 16:39:37 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::132 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:14973 On Thu, Dec 27, 2018 at 3:19 PM Martijn Dekker wrote: > > Op 27-12-18 om 19:22 schreef Chet Ramey: > > On 12/26/18 10:49 PM, Peng Yu wrote: > > > >> Although [[ -z ${1+s} ]] and (($#)) works for testing if $1 is set, > >> neither of them are uniformly better performance wise. In this case, > >> should [[ -v 1 ]] be supported? > > > > So you're saying that neither of the existing options performs better > > than the other, though they both perform well, so we should add some > > new capability just because? That's a particularly poor argument. > > Consistency might be a better argument. If [[ -v foo ]] is equivalent to > [[ -n ${foo+s} ]] for variables (with the advantage that you don't need > 'eval' to handle arbitrary values of 'foo'), then perhaps it's not > unreasonable to expect [[ -v 1 ]] to be equivalent to [[ -n ${1+s} ]]. > > FWIW, zsh and mksh do support this; ksh93 doesn't. The above are additions arguments that why [[ -v 1 ]] should be supported. What I meant in my original email is that I want something for testing if there is a command line argument (one or more, the exact number does not matter). $# gives more than that info, because it tells not only whether is any command line argument, but also how many. This could lead to slower performance if the goal is to just test if there is an argument. [[ -z ${1+s} ]] does something also more than necessary too, because it not only tests for whether $1 is set, it also replaced with a string "s". This also does more than just testing whether $1 is set. So both cases would be slower than [[ -v 1 ]] if it were supported. As of now, because (($#)) or [[ -z ${1+s} ]] are not consistently faster than the other, there is no way to write a program that is consistently fastest. To achieve this goal, one has to implement [[ -v 1 ]] or something similar that just test whether $1 but no more. -- Regards, Peng