Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

When we write programs that "learn", it turns out we do and they don't.


computers / comp.os.linux.misc / correct quoting for variables in bash scripts?

SubjectAuthor
* correct quoting for variables in bash scripts?Barry Stir
+- Re: correct quoting for variables in bash scripts?Aragorn
+* Re: correct quoting for variables in bash scripts?Ralf Fassel
|`- Re: correct quoting for variables in bash scripts?Jerry Peters
+- Re: correct quoting for variables in bash scripts?Carlos E. R.
+- Re: correct quoting for variables in bash scripts?Rich
+- Re: correct quoting for variables in bash scripts?Henning Hucke
`* Re: correct quoting for variables in bash scripts?Eli the Bearded
 `* Re: correct quoting for variables in bash scripts?Ralf Fassel
  `* Re: correct quoting for variables in bash scripts?Rich
   `* Re: correct quoting for variables in bash scripts?Ralf Fassel
    `* Re: correct quoting for variables in bash scripts?Lew Pitcher
     +* The difference between /bin/test and /bin/[Lew Pitcher
     |`- Re: The difference between /bin/test and /bin/[Eli the Bearded
     `- Re: correct quoting for variables in bash scripts?Ralf Fassel

1
correct quoting for variables in bash scripts?

<tije9a$19dt$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8402&group=comp.os.linux.misc#8402

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!aioe.org!TVR4W8nzk3t3s/u5DvDjIg.user.46.165.242.75.POSTED!not-for-mail
From: scoo...@mealteam6.mil (Barry Stir)
Newsgroups: comp.os.linux.misc
Subject: correct quoting for variables in bash scripts?
Date: Mon, 17 Oct 2022 19:28:39 +0800
Organization: Aioe.org NNTP Server
Message-ID: <tije9a$19dt$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="42429"; posting-host="TVR4W8nzk3t3s/u5DvDjIg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Firefox/68.0 SeaMonkey/2.53.14
X-Notice: Filtered by postfilter v. 0.9.2
X-Mozilla-News-Host: snews://nntp.aioe.org:563
 by: Barry Stir - Mon, 17 Oct 2022 11:28 UTC

I see various formats, such as
if [[ -L ${LINK1_DIR} ]]; then

if [[ -L "$LINK1_DIR" ]]; then

rather than just $LINK1_DIR
I presume this is insurance to ensure script works in every version of
Red Hat, Debian, Arch whatever...
So which is the ideal extra character to put around the environment
variable?

Re: correct quoting for variables in bash scripts?

<20221017141022.714d3043@nx-74205>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8403&group=comp.os.linux.misc#8403

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: telcon...@duck.com (Aragorn)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Mon, 17 Oct 2022 14:10:22 +0200
Organization: A noiseless patient Strider
Lines: 33
Message-ID: <20221017141022.714d3043@nx-74205>
References: <tije9a$19dt$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Injection-Info: reader01.eternal-september.org; posting-host="90651e06ca69cf14db26dfb987e3eb34";
logging-data="3355377"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/moyUiIsHQPRuqIHHfeY5k"
Cancel-Lock: sha1:IWkuaIJOu6/A3tgqNhLfUmxfios=
X-Newsreader: Claws Mail 4.1.0 (GTK 3.24.34; x86_64-pc-linux-gnu)
 by: Aragorn - Mon, 17 Oct 2022 12:10 UTC

On 17.10.2022 at 19:28, Barry Stir scribbled:

> I see various formats, such as
> if [[ -L ${LINK1_DIR} ]]; then
>
> if [[ -L "$LINK1_DIR" ]]; then
>
> rather than just $LINK1_DIR
> I presume this is insurance to ensure script works in every version
> of Red Hat, Debian, Arch whatever...
> So which is the ideal extra character to put around the environment
> variable?

There are different reasons for using either notation, and they are not
mutually exclusive. Portability between distributions is irrelevant in
this regard.

° The ${VARIABLE} notation bears preference due to the fact that it
protects against errors caused by concatenation.

° The "$VARIABLE" notation is used when there is a chance of the
variable containing spaces or other special characters, such as
e.g. a backslash or a newline character.

Therefore, one can combine the two formats as "${VARIABLE}" if
one suspects that the variable may contain spaces and/or other
special characters AND if the variable is to be concatenated with
either a literal or another variable.

--
With respect,
= Aragorn

Re: correct quoting for variables in bash scripts?

<ygamt9u8x4h.fsf@akutech.de>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8404&group=comp.os.linux.misc#8404

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!aioe.org!news.mixmin.net!news2.arglkargh.de!news.karotte.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralf...@gmx.de (Ralf Fassel)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Mon, 17 Oct 2022 14:15:10 +0200
Lines: 33
Message-ID: <ygamt9u8x4h.fsf@akutech.de>
References: <tije9a$19dt$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net hY+z9+K3lV+0gT2YZMXm7gngBtpsmLqyRQRnBVwKzKURO3Qgc=
Cancel-Lock: sha1:t/ZyxQuC/AyTDtsjPUx2dIGS8FA= sha1:XfbJoZ8THXb2e0V/BnfGNeowsWM=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)
 by: Ralf Fassel - Mon, 17 Oct 2022 12:15 UTC

* Barry Stir <scooter@mealteam6.mil>
| I see various formats, such as
| if [[ -L ${LINK1_DIR} ]]; then
>
| if [[ -L "$LINK1_DIR" ]]; then
>
| rather than just $LINK1_DIR
| I presume this is insurance to ensure script works in every version of
| Red Hat, Debian, Arch whatever...

Using "" also works if the variable is not set or empty, otherwise you
get a syntax error in that case.

Usually you need {} only when additional text should be appended to the
contents: ${LINK1_DIR}X (because without the {} the variable name would
be LINK1_DIRX).

| So which is the ideal extra character to put around the environment
| variable?

It depends :-)

"" also prevents file name globbing from happening, which might
or might not be what one wants.

$ touch foo1 foo2
$ a=foo\*
$ echo $a
foo1 foo2
$ echo "$a"
foo*

R'

Re: correct quoting for variables in bash scripts?

<jr50orF80uU2@mid.individual.net>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8405&group=comp.os.linux.misc#8405

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!aioe.org!news.mixmin.net!news2.arglkargh.de!news.karotte.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: robin_li...@es.invalid (Carlos E. R.)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Mon, 17 Oct 2022 14:39:55 +0200
Lines: 23
Message-ID: <jr50orF80uU2@mid.individual.net>
References: <tije9a$19dt$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net C3RgFGNW3bgW9Zv7MnBWVQM1RZjr91q6/X0UaB00P+zAix+A7s
Cancel-Lock: sha1:QxgQuLz6MP0HNZFUZE+uWzJrxDI=
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.2.2
Content-Language: es-ES, en-CA
In-Reply-To: <tije9a$19dt$1@gioia.aioe.org>
 by: Carlos E. R. - Mon, 17 Oct 2022 12:39 UTC

On 17/10/2022 13.28, Barry Stir wrote:
> I see various formats, such as
>   if [[ -L ${LINK1_DIR} ]]; then
>
>   if [[ -L "$LINK1_DIR" ]]; then
>
> rather than just $LINK1_DIR
> I presume this is insurance to ensure script works in every version of
> Red Hat, Debian, Arch whatever...

There are no differences among distributions. Bash is bash everywhere.

> So which is the ideal extra character to put around the environment
> variable?

Others have explained it well in their posts.

--
Cheers,
Carlos E.R.

Re: correct quoting for variables in bash scripts?

<tijn9n$3f5pf$2@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8406&group=comp.os.linux.misc#8406

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!paganini.bofh.team!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ric...@example.invalid (Rich)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Mon, 17 Oct 2022 14:02:32 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <tijn9n$3f5pf$2@dont-email.me>
References: <tije9a$19dt$1@gioia.aioe.org>
Injection-Date: Mon, 17 Oct 2022 14:02:32 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="a55705392c6aa52151c79328a213016e";
logging-data="3643183"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18TQiHH0HKYN68pF0bezB2A"
User-Agent: tin/2.0.1-20111224 ("Achenvoir") (UNIX) (Linux/3.10.17 (x86_64))
Cancel-Lock: sha1:ljPAHtY99N+Uoew7U7+F2UL9So8=
 by: Rich - Mon, 17 Oct 2022 14:02 UTC

Barry Stir <scooter@mealteam6.mil> wrote:
> I see various formats, such as
> if [[ -L ${LINK1_DIR} ]]; then
>
> if [[ -L "$LINK1_DIR" ]]; then
>
> rather than just $LINK1_DIR
> I presume this is insurance to ensure script works in every version of
> Red Hat, Debian, Arch whatever...
> So which is the ideal extra character to put around the environment
> variable?

The ${} form has a different meaning to the quotes (i.e., they do
different things).

The ${} form is defined in the bash man page this way:

${parameter}
The value of parameter is substituted. The braces are
required when parameter is a positional parameter with
more than one digit, or when parameter is followed by a
character which is not to be interpreted as part of its
name.

So the ${} form provides a way to delimit exactly the name of the
variable, without confusing it with further concatenated text.

The double quotes "" are for preventing the interpretation of special
characters that might be stored in the variable. Double qutoes are
defined this way in the bash man page:

Enclosing characters in double quotes preserves the literal
value of all characters within the quotes, with the exception of
$, `, \, and, when history expansion is enabled, !. The
characters $ and ` retain their special meaning within double
quotes. The backslash retains its special meaning only when
followed by one of the following characters: $, `, ", \, or
<newline>. A double quote may be quoted within double quotes by
preceding it with a backslash. If enabled, history expansion
will be performed unless an ! appearing in double quotes is
escaped using a backslash. The backslash preceding the ! is
not removed.

So, if one is not appending text to the variable, one only needs to do:

"$LINK1_DIR"

The {}'s are redundant in that case. However there are some folks who
seem to like the look of ${...} and always include the {}'s.

The double quotes, however, are in most cases, a very good idea to
always include, because if you leave them off, and one day the variable
contains a space (or other character special to the shell) then things
will break, often badly.

Re: correct quoting for variables in bash scripts?

<tijmqt$la7$1@sirius.aeon.icebear.cloud>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8407&group=comp.os.linux.misc#8407

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: h_hucke+...@newsmail.aeon.icebear.org (Henning Hucke)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Mon, 17 Oct 2022 13:54:37 -0000 (UTC)
Organization: aeon: think longer than you thought before
Lines: 35
Distribution: world
Message-ID: <tijmqt$la7$1@sirius.aeon.icebear.cloud>
References: <tije9a$19dt$1@gioia.aioe.org>
Reply-To: Henning Hucke <h_hucke+news.reply@newsmail.aeon.icebear.org>
X-Trace: individual.net fG2SRh08z4iSDgcEWpTfxA8c/uuIRARUCcuqvHkQL8FAFxkNsP
X-Orig-Path: news.aeon.icebear.cloud!news1.aeon.icebear.cloud!.POSTED.romulus.aeon.icebear.cloud!not-for-mail
Cancel-Lock: sha1:URahCdIxhXoXpSah0CJeErYBDQI= sha1:BMDd7yYSxzqRj0Br94KxHvKjOaU=
Injection-Date: Mon, 17 Oct 2022 13:54:37 -0000 (UTC)
Injection-Info: sirius.aeon.icebear.cloud; posting-host="romulus.aeon.icebear.cloud:fd09:afca:b044:1:4ecc:6aff:fecf:5c8f";
logging-data="21831"; mail-complaints-to="abuse+news@aeon.icebear.cloud"
User-Agent: tin/2.4.1-20161224 ("Daill") (UNIX) (Linux/4.9.0-15-amd64 (x86_64))
 by: Henning Hucke - Mon, 17 Oct 2022 13:54 UTC

Barry Stir <scooter@mealteam6.mil> wrote:
> I see various formats, such as
> if [[ -L ${LINK1_DIR} ]]; then
>
> if [[ -L "$LINK1_DIR" ]]; then
>
> rather than just $LINK1_DIR
> I presume this is insurance to ensure script works in every version of
> Red Hat, Debian, Arch whatever...
> So which is the ideal extra character to put around the environment
> variable?

Hi Barry.

be aware that these forms are not equivalent.

"$LINK1_DIR" and "${LINK1_DIR}" are - kind of - equivalent and
$LINK1_DIR and ${LINK1_DIR} are.

The version with curly brackets is more secure since if you have a
variable "LINK1" defined

echo "-$LINK1_DIR-"

either triggers an error or just prints "--" while

echo "-${LINK1}_DIR-"

prints "-<LINK1 contents>_DIR-" and vice versa if you have a variable
"LINK1_DIR" defined.

Regards
Henning
--
It doesn't matter whether you win or lose -- until you lose.

Re: correct quoting for variables in bash scripts?

<eli$2210171923@qaz.wtf>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8409&group=comp.os.linux.misc#8409

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!panix!.POSTED.panix5.panix.com!qz!not-for-mail
From: *...@eli.users.panix.com (Eli the Bearded)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Mon, 17 Oct 2022 23:35:12 -0000 (UTC)
Organization: Some absurd concept
Message-ID: <eli$2210171923@qaz.wtf>
References: <tije9a$19dt$1@gioia.aioe.org>
Injection-Date: Mon, 17 Oct 2022 23:35:12 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="panix5.panix.com:166.84.1.5";
logging-data="12100"; mail-complaints-to="abuse@panix.com"
User-Agent: Vectrex rn 2.1 (beta)
X-Liz: It's actually happened, the entire Internet is a massive game of Redcode
X-Motto: "Erosion of rights never seems to reverse itself." -- kenny@panix
X-US-Congress: Moronic Fucks.
X-Attribution: EtB
XFrom: is a real address
Encrypted: double rot-13
 by: Eli the Bearded - Mon, 17 Oct 2022 23:35 UTC

In comp.os.linux.misc, Barry Stir <scooter@mealteam6.mil> wrote:
> I see various formats, such as
> if [[ -L ${LINK1_DIR} ]]; then
>
> if [[ -L "$LINK1_DIR" ]]; then
>
> rather than just $LINK1_DIR I presume this is insurance to ensure
> script works in every version of Red Hat, Debian, Arch whatever... So
> which is the ideal extra character to put around the environment
> variable?

As others have mentioned, ${} is to force parsing when being next to
something could confuse the variable name. The "$" version is to
force an empty tring to exist in contexts where it might get lost.

In your example neither is needed. Firstly there's nothing adjoining to
confuse the variable name. Secondly, it is [[ not [.

The one [ ] square bracket form of the test command needs empty string
protection because variable expansion happens before thecommand is run.

The two [[ ]] square bracket form of the test command is explicitly a
shell built-in, so it can recognize empty strings without help.

Elijah
------
${} also gets used for manipulations like "base=${path##*/}"

Re: correct quoting for variables in bash scripts?

<ygailkh8om8.fsf@akutech.de>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8410&group=comp.os.linux.misc#8410

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralf...@gmx.de (Ralf Fassel)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Tue, 18 Oct 2022 11:31:11 +0200
Lines: 26
Message-ID: <ygailkh8om8.fsf@akutech.de>
References: <tije9a$19dt$1@gioia.aioe.org> <eli$2210171923@qaz.wtf>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net MtpYbseIDS6Fw/4Ph0G6KA1SXG+zYp/z149fO3rQekUKsKdYI=
Cancel-Lock: sha1:78Hh6TdjE+JGi+Oa7g9oAbwtYDI= sha1:9zr9sYDeyKx/VzSYtd6vQOt1nm0=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)
 by: Ralf Fassel - Tue, 18 Oct 2022 09:31 UTC

* Eli the Bearded <*@eli.users.panix.com>
| In your example neither is needed. Firstly there's nothing adjoining to
| confuse the variable name. Secondly, it is [[ not [.
>
| The one [ ] square bracket form of the test command needs empty string
| protection because variable expansion happens before thecommand is run.
>
| The two [[ ]] square bracket form of the test command is explicitly a
| shell built-in, so it can recognize empty strings without help.

Interesting, didn't know that difference:

$ unset bar
$ [[ $bar = x ]] && echo yes
$ type [[
[[ is a shell keyword

$ [ $bar = x ] && echo yes
bash: [: =: unary operator expected
$ type [
[ is a shell builtin

Most probably the error thrown with "[" is to ensure compatibility with
/usr/bin/test?

R'

Re: correct quoting for variables in bash scripts?

<tim4kr$3nq91$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8411&group=comp.os.linux.misc#8411

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ric...@example.invalid (Rich)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Tue, 18 Oct 2022 12:02:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <tim4kr$3nq91$1@dont-email.me>
References: <tije9a$19dt$1@gioia.aioe.org> <eli$2210171923@qaz.wtf> <ygailkh8om8.fsf@akutech.de>
Injection-Date: Tue, 18 Oct 2022 12:02:35 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="586993c884092a46d9c089cb681c5b5a";
logging-data="3926305"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GleAZLnHg5QNa4XVE6zIL"
User-Agent: tin/2.0.1-20111224 ("Achenvoir") (UNIX) (Linux/3.10.17 (x86_64))
Cancel-Lock: sha1:vyQom1wcoxNiaZdIMyvRS6eN6g4=
 by: Rich - Tue, 18 Oct 2022 12:02 UTC

Ralf Fassel <ralfixx@gmx.de> wrote:
> | The two [[ ]] square bracket form of the test command is explicitly a
> | shell built-in, so it can recognize empty strings without help.
>
> Interesting, didn't know that difference:
>
> $ unset bar
> $ [[ $bar = x ]] && echo yes
> $ type [[
> [[ is a shell keyword
>
> $ [ $bar = x ] && echo yes
> bash: [: =: unary operator expected
> $ type [
> [ is a shell builtin
>
> Most probably the error thrown with "[" is to ensure compatibility with
> /usr/bin/test?

The error thrown with "[" is because what "[" sees as its argv is "= x"
because the shell has already converted $bar into "" before "[" is
executed.

Its complaint is that it found an operator it did not expect (the "=") in
the argv[0] slot. The error message is mathematically accurate, but
very confusing to most users not well versed in math operator language
("=" being a "binary operator" not a "unary operator").

Re: correct quoting for variables in bash scripts?

<ygaedv58ewi.fsf@akutech.de>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8412&group=comp.os.linux.misc#8412

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralf...@gmx.de (Ralf Fassel)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Tue, 18 Oct 2022 15:01:01 +0200
Lines: 42
Message-ID: <ygaedv58ewi.fsf@akutech.de>
References: <tije9a$19dt$1@gioia.aioe.org> <eli$2210171923@qaz.wtf>
<ygailkh8om8.fsf@akutech.de> <tim4kr$3nq91$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net GvLCPTIAVOnoVD4db03RiQ+oTuP5TsE9oYsDcKCQvQ14Al1rE=
Cancel-Lock: sha1:t8akX4MIaE5ptUdkE+sWP1g5WiE= sha1:UBJaLOZNKBvpekU2KZOaEGAgF4E=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)
 by: Ralf Fassel - Tue, 18 Oct 2022 13:01 UTC

* Rich <rich@example.invalid>
| Ralf Fassel <ralfixx@gmx.de> wrote:
| > | The two [[ ]] square bracket form of the test command is explicitly a
| > | shell built-in, so it can recognize empty strings without help.
| >
| > Interesting, didn't know that difference:
| >
| > $ unset bar
| > $ [[ $bar = x ]] && echo yes
| > $ type [[
| > [[ is a shell keyword
| >
| > $ [ $bar = x ] && echo yes
| > bash: [: =: unary operator expected
| > $ type [
| > [ is a shell builtin
| >
| > Most probably the error thrown with "[" is to ensure compatibility with
| > /usr/bin/test?
>
| The error thrown with "[" is because what "[" sees as its argv is "= x"
| because the shell has already converted $bar into "" before "[" is
| executed.

I understand the problem for the case that there is a command to be
called after variable substitution.

But the original theory (first quotation above) why [[ did not throw an
error was that "... ([[) is explicitly a shell built-in, so it can
recognize empty strings without help."

Since '[' is also flagged as a shell-builtin by bash itself ("type [ =>
shell builtin"), I wondered "can't it detect it then just the same?"

There seems to be a difference between shell-keywords here and
shell-builtins when it comes to the handling of empty variables.

(Of course there is also /usr/bin/[ and /usr/bin/test (2 different
programs on my Opensuse) but I suppose that these are not called
when "[" is a shell-builtin.)

R'

Re: correct quoting for variables in bash scripts?

<timcia$3o485$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8414&group=comp.os.linux.misc#8414

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Tue, 18 Oct 2022 14:17:46 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <timcia$3o485$1@dont-email.me>
References: <tije9a$19dt$1@gioia.aioe.org> <eli$2210171923@qaz.wtf>
<ygailkh8om8.fsf@akutech.de> <tim4kr$3nq91$1@dont-email.me>
<ygaedv58ewi.fsf@akutech.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 18 Oct 2022 14:17:46 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="f3d0c8a5b9477cdf064d5e8fcb2a4afa";
logging-data="3936517"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ikf/vAoWwZfi904E6BuH6I4V6Obr0cpI="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:fzIDqcfINKIWybOJlxeKxiS2m98=
 by: Lew Pitcher - Tue, 18 Oct 2022 14:17 UTC

On Tue, 18 Oct 2022 15:01:01 +0200, Ralf Fassel wrote:

> * Rich <rich@example.invalid>
> | Ralf Fassel <ralfixx@gmx.de> wrote:
> | > | The two [[ ]] square bracket form of the test command is explicitly a
> | > | shell built-in, so it can recognize empty strings without help.
> | >
> | > Interesting, didn't know that difference:
> | >
> | > $ unset bar
> | > $ [[ $bar = x ]] && echo yes
> | > $ type [[
> | > [[ is a shell keyword
> | >
> | > $ [ $bar = x ] && echo yes
> | > bash: [: =: unary operator expected
> | > $ type [
> | > [ is a shell builtin
> | >
> | > Most probably the error thrown with "[" is to ensure compatibility with
> | > /usr/bin/test?
>>
> | The error thrown with "[" is because what "[" sees as its argv is "= x"
> | because the shell has already converted $bar into "" before "[" is
> | executed.
>
> I understand the problem for the case that there is a command to be
> called after variable substitution.
>
> But the original theory (first quotation above) why [[ did not throw an
> error was that "... ([[) is explicitly a shell built-in, so it can
> recognize empty strings without help."

[[ is not a "shell builtin", it is a "shell keyword". That makes a difference.

The shell parser handles keywords earlier on in the process than it handles
builtins; more to the point, it handles keywords /before/ it performs $ expansion,
and it handles builtins /after/ it performs $ expansion.

> Since '[' is also flagged as a shell-builtin by bash itself ("type [ =>
> shell builtin"), I wondered "can't it detect it then just the same?"

Not by the current syntax definition of the shell. As the shell must be
able to disable a "builtin" command, and use the external in it's place,
the builtin command must accept the same arguments in the same form as
the external command. That implies that the shell must treat the arguments
of a builtin command in the same way it would treat the arguments of
an external command, and that includes $ expansion.

> There seems to be a difference between shell-keywords here and
> shell-builtins when it comes to the handling of empty variables.
>
> (Of course there is also /usr/bin/[ and /usr/bin/test (2 different
> programs on my Opensuse)

Although, they /don't/ have to be different programs. Historically,
the [ command was just an alias (a hardlink or softlink) of the test
command. I don't know why that changed.

> but I suppose that these are not called when "[" is a shell-builtin.

You can disable the [ builtin using the "enable" builtin ("enable -n [")
and then your script will search for and call the external [ command.

HTH
--
Lew Pitcher
"In Skills, We Trust"

The difference between /bin/test and /bin/[

<timeu8$3o485$2@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8415&group=comp.os.linux.misc#8415

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.os.linux.misc
Subject: The difference between /bin/test and /bin/[
Date: Tue, 18 Oct 2022 14:58:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <timeu8$3o485$2@dont-email.me>
References: <tije9a$19dt$1@gioia.aioe.org> <eli$2210171923@qaz.wtf>
<ygailkh8om8.fsf@akutech.de> <tim4kr$3nq91$1@dont-email.me>
<ygaedv58ewi.fsf@akutech.de> <timcia$3o485$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 18 Oct 2022 14:58:16 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="f3d0c8a5b9477cdf064d5e8fcb2a4afa";
logging-data="3936517"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18BA7tDqWobvEqhj+CvFmOuZ5/AoHgAQgA="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:HDjVa3TGxqE61bcYTxRX68/Ssjc=
 by: Lew Pitcher - Tue, 18 Oct 2022 14:58 UTC

On Tue, 18 Oct 2022 14:17:46 +0000, Lew Pitcher wrote:

> On Tue, 18 Oct 2022 15:01:01 +0200, Ralf Fassel wrote:
[snip]
>> (Of course there is also /usr/bin/[ and /usr/bin/test (2 different
>> programs on my Opensuse)
>
> Although, they /don't/ have to be different programs. Historically,
> the [ command was just an alias (a hardlink or softlink) of the test
> command. I don't know why that changed.

The question intrigued me, so I checked the source for the /bin/test
on my Slackware installation. It appears that, for the GNU CoreUtils
version of test, GNU took "the long way around" in it's error reporting.

Specifically, the source includes conditional compilation logic to
report name of the binary based on a value passed in at compilation time.
So, the compilation of test.c produces one of two slightly different
binaries (only the embedded program name changes), depending on whether
the compile includes a -DLBRACKET=1 or not.

The "classic Unix" way to determine the binary's name was to check or
report the contents of argv[0], with no difference in the binaries of
/bin/test and /bin/[ .

[snip]
--
Lew Pitcher
"In Skills, We Trust"

Re: correct quoting for variables in bash scripts?

<ygaa65s86z1.fsf@akutech.de>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8416&group=comp.os.linux.misc#8416

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralf...@gmx.de (Ralf Fassel)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Wed, 19 Oct 2022 12:04:34 +0200
Lines: 12
Message-ID: <ygaa65s86z1.fsf@akutech.de>
References: <tije9a$19dt$1@gioia.aioe.org> <eli$2210171923@qaz.wtf>
<ygailkh8om8.fsf@akutech.de> <tim4kr$3nq91$1@dont-email.me>
<ygaedv58ewi.fsf@akutech.de> <timcia$3o485$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net SGT1uys+VoKCaE344W3Q+QVA5vFJSnYuoi5BaIwGGiDKnlinA=
Cancel-Lock: sha1:HecI3om6NlI6uLZVak+p9acSAC0= sha1:ZTOYHMqFx7C1IsW3om4Ru0fRhwQ=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)
 by: Ralf Fassel - Wed, 19 Oct 2022 10:04 UTC

* Lew Pitcher <lew.pitcher@digitalfreehold.ca>
| > But the original theory (first quotation above) why [[ did not throw an
| > error was that "... ([[) is explicitly a shell built-in, so it can
| > recognize empty strings without help."
>
| [[ is not a "shell builtin", it is a "shell keyword". That makes a
| difference.
--<snip-snip>--

Thanks, very informative!

R'

Re: The difference between /bin/test and /bin/[

<eli$2210210026@qaz.wtf>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8425&group=comp.os.linux.misc#8425

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!panix!.POSTED.panix5.panix.com!qz!not-for-mail
From: *...@eli.users.panix.com (Eli the Bearded)
Newsgroups: comp.os.linux.misc
Subject: Re: The difference between /bin/test and /bin/[
Date: Fri, 21 Oct 2022 04:41:41 -0000 (UTC)
Organization: Some absurd concept
Message-ID: <eli$2210210026@qaz.wtf>
References: <tije9a$19dt$1@gioia.aioe.org> <ygaedv58ewi.fsf@akutech.de> <timcia$3o485$1@dont-email.me> <timeu8$3o485$2@dont-email.me>
Injection-Date: Fri, 21 Oct 2022 04:41:41 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="panix5.panix.com:166.84.1.5";
logging-data="18213"; mail-complaints-to="abuse@panix.com"
User-Agent: Vectrex rn 2.1 (beta)
X-Liz: It's actually happened, the entire Internet is a massive game of Redcode
X-Motto: "Erosion of rights never seems to reverse itself." -- kenny@panix
X-US-Congress: Moronic Fucks.
X-Attribution: EtB
XFrom: is a real address
Encrypted: double rot-13
 by: Eli the Bearded - Fri, 21 Oct 2022 04:41 UTC

In comp.os.linux.misc, Lew Pitcher <lew.pitcher@digitalfreehold.ca> wrote:
> The question intrigued me, so I checked the source for the /bin/test
> on my Slackware installation. It appears that, for the GNU CoreUtils
> version of test, GNU took "the long way around" in it's error reporting.
>
> Specifically, the source includes conditional compilation logic to
> report name of the binary based on a value passed in at compilation time.

Time was when many non-Linux systems used to have Gnu utilities
installed with different filenames, typically adding a 'g' to the front.

On this NetBSD box I'm using, for example:

$ ls --version
ls: unknown option -- -
usage: ls [-1AaBbCcdFfghikLlMmnOoPpqRrSsTtuWwXx] [file ...]

$ gls --version 2>&1
ls (GNU coreutils) 9.0
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.

I expect the compiled in name is relevant to that. Not sure how test
uses it though.

$ cp $(which gtest) /tmp/gnutest
$ /tmp/gnutest x =
gnutest: missing argument after '='

Elijah
------
gcc is called that because the vendor cc was likely different

Re: correct quoting for variables in bash scripts?

<tjrvfe$rde6$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=8596&group=comp.os.linux.misc#8596

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: jer...@example.invalid (Jerry Peters)
Newsgroups: comp.os.linux.misc
Subject: Re: correct quoting for variables in bash scripts?
Date: Tue, 1 Nov 2022 20:27:26 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <tjrvfe$rde6$1@dont-email.me>
References: <tije9a$19dt$1@gioia.aioe.org> <ygamt9u8x4h.fsf@akutech.de>
Injection-Date: Tue, 1 Nov 2022 20:27:26 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="02a42fcd52f44c0cb6cdc62bd32c01f7";
logging-data="898502"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18//0aHBnOWIZARQMadZ154uccuCIoRrLA="
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (Linux/5.10.142 (x86_64))
Cancel-Lock: sha1:MLEDdVM61yMP+paVzzRcGij0VIs=
 by: Jerry Peters - Tue, 1 Nov 2022 20:27 UTC

Ralf Fassel <ralfixx@gmx.de> wrote:
> * Barry Stir <scooter@mealteam6.mil>
> | I see various formats, such as
> | if [[ -L ${LINK1_DIR} ]]; then
>>
> | if [[ -L "$LINK1_DIR" ]]; then
>>
> | rather than just $LINK1_DIR
> | I presume this is insurance to ensure script works in every version of
> | Red Hat, Debian, Arch whatever...
>
> Using "" also works if the variable is not set or empty, otherwise you
> get a syntax error in that case.

Not with [[:
~$ unset x
~$ [[ -L $x ]] || echo false
false
But it's still a good idea to quote variables unless you explicitely
need the unquoted behaviour, like path expansion.

>
> Usually you need {} only when additional text should be appended to the
> contents: ${LINK1_DIR}X (because without the {} the variable name would
> be LINK1_DIRX).
>
> | So which is the ideal extra character to put around the environment
> | variable?
>
> It depends :-)
>
> "" also prevents file name globbing from happening, which might
> or might not be what one wants.
>
> $ touch foo1 foo2
> $ a=foo\*
> $ echo $a
> foo1 foo2
> $ echo "$a"
> foo*
>
> R'

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor