0

Running PHP script from cli as:

$ php test.php {a=1,b=2,c=3}

Results in $argv structure as:

[
    0 => test.php
    1 => a=1
    2 => b=2
    3 => c=3
]

Question.

  1. What is the intended meaning of { ... } in that case?
  2. Is it a PHP thing or bash thing?
  3. Is there an online documentation that describes this behaviour? (yes, I've googled already - without success)

Thanks!

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
temuri
  • 2,767
  • 5
  • 41
  • 63

2 Answers2

1

Per section 3.5.1 of the Bash manual:

Brace expansion is a mechanism by which arbitrary strings may be generated. This mechanism is similar to filename expansion, but the filenames generated need not exist. Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a sequence expression between a pair of braces, followed by an optional postscript. The preamble is prefixed to each string contained within the braces, and the postscript is then appended to each resulting string, expanding left to right.

In your case, there is no preamble or postscript, so it's just expanding to each of the elements in the list.

You can see the result by using echo:

echo {a=1,b=2,c=3}

Which outputs:

a=1 b=2 c=3

If you were to use a preamble and postscript:

echo before{a=1,b=2,c=3}after

You get:

beforea=1after beforeb=2after beforec=3after

I typically use this when trying to copy or move a file to a backup:

cp somefile.txt{,.bak}

Which expands to:

cp somefile.txt somefile.txt.bak
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
-1

{} is represented as an object in PHP, when var_dump an instance of a class, you will se it's wrapped by {}. There is a documentation i found which describe how to use $argv https://www.php.net/manual/en/reserved.variables.argv.php

Lam Nguyen
  • 186
  • 1
  • 8