# Double expansion/expansion inside an expansion?

**URL:** <https://discuss.kakoune.com/t/double-expansion-expansion-inside-an-expansion/1209>\
**Category:** Help\
**Tags:** language\
**Created:** [August 14, 2020, 5:03am UTC](https://discuss.kakoune.com/t/double-expansion-expansion-inside-an-expansion/1209 "2020-08-14T05:03:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ldld](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/ldld/32/625_2.png) [@ldld](https://discuss.kakoune.com/u/ldld)\
**Post date:** [August 14, 2020, 5:03am UTC](https://discuss.kakoune.com/t/double-expansion-expansion-inside-an-expansion/1209/1 "2020-08-14T05:03:49Z")

</div>

I am trying to use options with names depending on user input ( that is, argument to a command), since there is no arrays or other collection types in kakoune.  
So far I have been able to create such options with

> declare-option str “bridge\_%arg{1}\_pid”  
> and when I call  
> my-command AAA  
> I get an option named bridge\_AAA\_pid

However I don’t know how to use the content of the created option, in either %{} or %sh{} blocks.  
So far I’ve tried

> define-command -override test-pid -params 1 %{  
> echo %opt{bridge\_%arg{1}_pid}  
> echo %opt{bridge_"%arg{1}“_pid}  
> echo %opt{"bridge_%arg{1}_pid"}  
> echo %opt"{bridge_%arg{1}_pid}"  
> echo "%opt{bridge_%arg{1}\_pid}”  
> }  
> test-pid AAA  
> but none of them worked.

---

<div class="post-metadata">

**Author:** ![Screwtapello](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/screwtapello/32/12_2.png) [@Screwtapello](https://discuss.kakoune.com/u/Screwtapello)\
**Post date:** [August 14, 2020, 6:12am UTC](https://discuss.kakoune.com/t/double-expansion-expansion-inside-an-expansion/1209/2 "2020-08-14T06:12:36Z")

</div>

Unlike POSIX shell, which has multiple expansion passes and thus frequently allows things to be expanded more times than the programmer expects, Kakoune is very careful to only ever expand things once, unless you ask for it specifically. That means you can’t do the kind of thing you’re trying to do deliberately, but it also means you can’t do it accidentally, so overall I think it’s a win.

If you want multiple rounds of expansion, you’re going to need to use the `eval` command, something like this:

```auto
define-command -override test-pid -params 1 %{
    eval "echo %%opt{bridge_%arg{1}_pid}"
}

```

Calling `:test-pid AAA` will result in the following transformations:

- `eval "echo %%opt{bridge_%arg{1}_pid}"`
- `eval 'echo %opt{bridge_AAA_pid}'`
- `echo %opt{bridge_AAA_pid}`
- `echo THE_VALUE`

…assuming the current value of `bridge_AAA_pid` is “THE\_VALUE”.

---

<div class="post-metadata">

**Author:** ![ldld](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/ldld/32/625_2.png) [@ldld](https://discuss.kakoune.com/u/ldld)\
**Post date:** [August 14, 2020, 6:24am UTC](https://discuss.kakoune.com/t/double-expansion-expansion-inside-an-expansion/1209/3 "2020-08-14T06:24:53Z")

</div>

Thank you very much.

The problem is now solved by

> declare-option str bridge\_pid\_temp  
> define-command -override test-pid -params 1 %{  
> eval “set-option global bridge\_pid\_temp %%opt{bridge\_%arg{1}\_pid}”  
> do-whatever-with %opt{bridge\_pid\_temp}  
> echo %sh{ echo $kak\_opt\_bridge\_pid\_temp now accessible in shell}  
> }  
> test-pid AAA

Though I wonder if there is a solution without the intermediate bridge\_pid\_temp.
