mainThe GNU Bourne-Again SHell - Support: sr #109348, [Syntax extension] Nested...

 
 

sr #109348: [Syntax extension] Nested transformations within variable expansions

Submitter:  None
Submitted:  Sun 09 Jul 2017 05:56:12 PM UTC
   
 
Category:  None Priority:  5 - Normal
Severity:  2 - Minor Status:  Wont Do
Privacy:  Public Assigned to:  None
Originator Email:  -email is unavailable- Open/Closed:  Open
Operating System:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Fri 31 Dec 2021 07:21:19 AM UTC, comment #1: 

While I agree that this suggestion is an improvement over the ${${var#prefix}%suffix} notation suggested by some folk, I would rather reserve the ${{...}} syntax for a more general expansion syntax, perhaps along the lines of the syntax used in Exim's configuration file.

To a first approximation, Exim allows ${var modifiers...} and ${verb parameters...} where modifiers or parameters might incorporate nested expansions.

Exim knows all its verbs and variables, so there's no ambiguity, whereas I would suggest marking verbs to distinguish them from variable names, perhaps like this:


${{ var modifier1 modifier2 modifier3 ... }}
${{ var ! [3] ! /{#prefix}{} /{%suffix}{} /{pattern}{replacement} split{" "} glob }}

srcs=( src/*.c )
command "${{ srcs [@] /{##*/} /{%.c}{.o} }}"

${{if: condition {then_value} }}
${{if: condition {then_value} condition2 {then_value2} ... {else_value} }}

${{printf: {formatstring} {arg1} {arg2} {arg3} ...}}


The parts between {{ }} would be their own grammar, not directly subject to other expansions; only the parts inside the inner {} would be subject to further expansions.

The conditions would be numeric expressions, but I would add "[{string} = {string}]" as a term to that as well.

(Indirection "!", array indexing "[n]", array slicing "[n:m]", word-splitting "@", globbing "*", and counting "#", would all count as modifiers, and apply to all the words so far constructed.)

Martin D Kealey <kurahaupo>
Sun 09 Jul 2017 05:56:12 PM UTC, original submission:  

Motivation :

The current variable expansion syntax allows one to perform very useful substitutions (such as removing a prefix or suffix, or replacing one) on a one-off basis. However, it only allows a single such substitution to be performed at a time. If I have a variable in which I need to replace both a prefix and a suffix, I have no choice but to use an intermediate variable :

  srcs=( src/*.c )
  x=( "${srcs[@]/#$SRC/$OBJ}" )
  command ... "${x[@]/%.c/.o}"

I would propose that the previous example be expressible as follows, in a single expansion :

  srcs=( src/*.c )
  command ... "${{srcs[@]/#$SRC/$OBJ}/%.c/.o}"

The substitutions would take place in a left-to-right (or innermost-to-outermost) order, on every element of the leftmost array or variable name, just like they do now.

As far as I know, the nested braces don't clash with any existing variable-name syntax, and since they are already a delimiting element of parameter patterns, they arent likely to clash with pattern syntaxes either.

Thank you for your attention, and keep up the good work
Marc


Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Updated the item)
  • -email is unavailable- added by kurahaupo (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-01-17 chet StatusNone Wont Do

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code