help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Shopt to change default connector to '&&'


From: Greg Wooledge
Subject: Re: Shopt to change default connector to '&&'
Date: Wed, 8 Mar 2023 11:04:06 -0500

On Wed, Mar 08, 2023 at 04:08:30PM +0100, Hugues Morisset wrote:
> Hello, It would be great that users could choose to use '&&' as a command
> connector instead of ';'.

What problem are you trying to solve?  I don't quite understand the
context for your change.

> +@item default_connector_and_and
> +If set, Bash
> +will use '&&' as a command separator instead
> +of ';' for command lines with no specified
> +separator.
> +

This doesn't clear anything up for me.  After reading that, I still
lack all context for what you're trying to change, and what your change
actually does.

> --- parse.y
> +++ parse.y
> @@ -248,6 +248,9 @@ char *current_prompt_string;
>  /* Non-zero means we expand aliases in commands. */
>  int expand_aliases = 0;
>  
> +/* Token used to connect commands separated by a newline */
> +int default_connector = ';';
> +
>  /* If non-zero, the decoded prompt string undergoes parameter and
>     variable substitution, command substitution, arithmetic substitution,
>     string expansion, process substitution, and quote removal in
> @@ -1168,7 +1171,7 @@ list1:          list1 AND_AND newline_list list1
>                         if (parser_state & PST_CMDSUBST)
>                           $$ = command_connect ($1, $4, '\n');
>                         else
> -                         $$ = command_connect ($1, $4, ';');
> +                         $$ = command_connect ($1, $4, default_connector);
>                       }
>       |       pipeline_command
>                       { $$ = $1; }

This is the meat of it, right?  You're changing part of the definition
of a compound list:


/* A list allows leading or trailing newlines and
   newlines as operators (equivalent to semicolons).
   It must end with a newline or semicolon.
   Lists are used within commands such as if, for, while.  */

compound_list:  newline_list list0
                        {
                          $$ = $2;
                          if (need_here_doc && last_read_token == '\n')
                            gather_here_documents ();
                         }
        |       newline_list list1
                        {
                          $$ = $2;
                        }
        ;


But... why?  What does your change allow you to do, which you couldn't
do previously?

An unmodified bash *already* lets you write code like this:

if cmd1 && cmd2 && cmd3; then
    cmd4 && cmd5
fi

It also lets you write code like this:

if cmd1; cmd2; cmd3; then
    cmd4; cmd5
fi

These two pieces of code have different meanings, of course.  The author
of the script gets to choose which one is desired.

Maybe I'm just missing something here.  Can you please explain in a little
bit more detail?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]