Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.misc > #35744

Re: correct quoting for variables in bash scripts?

From Lew Pitcher <lew.pitcher@digitalfreehold.ca>
Newsgroups comp.os.linux.misc
Subject Re: correct quoting for variables in bash scripts?
Date 2022-10-18 14:17 +0000
Organization A noiseless patient Spider
Message-ID <timcia$3o485$1@dont-email.me> (permalink)
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>

Show all headers | View raw


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"

Back to comp.os.linux.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

correct quoting for variables in bash scripts? Barry Stir <scooter@mealteam6.mil> - 2022-10-17 19:28 +0800
  Re: correct quoting for variables in bash scripts? Aragorn <telcontar@duck.com> - 2022-10-17 14:10 +0200
  Re: correct quoting for variables in bash scripts? Ralf Fassel <ralfixx@gmx.de> - 2022-10-17 14:15 +0200
    Re: correct quoting for variables in bash scripts? Jerry Peters <jerry@example.invalid> - 2022-11-01 20:27 +0000
  Re: correct quoting for variables in bash scripts? "Carlos E. R." <robin_listas@es.invalid> - 2022-10-17 14:39 +0200
  Re: correct quoting for variables in bash scripts? Rich <rich@example.invalid> - 2022-10-17 14:02 +0000
  Re: correct quoting for variables in bash scripts? Henning Hucke <h_hucke+spam.news@newsmail.aeon.icebear.org> - 2022-10-17 13:54 +0000
  Re: correct quoting for variables in bash scripts? Eli the Bearded <*@eli.users.panix.com> - 2022-10-17 23:35 +0000
    Re: correct quoting for variables in bash scripts? Ralf Fassel <ralfixx@gmx.de> - 2022-10-18 11:31 +0200
      Re: correct quoting for variables in bash scripts? Rich <rich@example.invalid> - 2022-10-18 12:02 +0000
        Re: correct quoting for variables in bash scripts? Ralf Fassel <ralfixx@gmx.de> - 2022-10-18 15:01 +0200
          Re: correct quoting for variables in bash scripts? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-10-18 14:17 +0000
            The difference between /bin/test and /bin/[ Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-10-18 14:58 +0000
              Re: The difference between /bin/test and /bin/[ Eli the Bearded <*@eli.users.panix.com> - 2022-10-21 04:41 +0000
            Re: correct quoting for variables in bash scripts? Ralf Fassel <ralfixx@gmx.de> - 2022-10-19 12:04 +0200

csiph-web