Question: Currently I use the following command to export the environment variable `FOO`:

```
export FOO=$(for i in {0..7}; do echo -n "uops_dispatched_port.port_$i|p$i,"; done)

```

This results in the string:

```
uops_dispatched_port.port_0|p0,uops_dispatched_port.port_1|p1,uops_dispatched_port.port_2|p2,uops_dispatched_port.port_3|p3,uops_dispatched_port.port_4|p4,uops_dispatched_port.port_5|p5,uops_dispatched_port.port_6|p6,uops_dispatched_port.port_7|p7,

```

I'd like a similarly simple, DRY, command that results in the same string without the trailing comma.

Answer: 
Would you please try the following:

```
export FOO=$(a=(); for i in {0..7}; do a+=("uops_dispatched_port.port_$i|p$i"); done; IFS=,; echo "${a[*]}")

```

The assignment to `IFS` is limited within the subshell and does not affect the following statements.
Instead of printing the comma at the end of each `echo` string (and then removing the last one), print the comma at the beginning of the `echo` string for the 2nd thru 7th passes through the loop ...

* remove comma (`,`) from `echo` command
* set variable `dl=""` for first pass through loop
* print `${dl}` at beginning of `echo` string
* set `dl=","` at end of loop so that ...
* on successive passes through the loop the `echo` string starts with a comma (`,`)

This gives us:

```
$ export FOO=$(dl="" ; for i in {0..7}; do echo -n "${dl}uops_dispatched_port.port_$i|p$i"; dl=","; done)
$ echo $FOO
uops_dispatched_port.port_0|p0,uops_dispatched_port.port_1|p1,uops_dispatched_port.port_2|p2,uops_dispatched_port.port_3|p3,uops_dispatched_port.port_4|p4,uops_dispatched_port.port_5|p5,uops_dispatched_port.port_6|p6,uops_dispatched_port.port_7|p7

```

**NOTE**: I just noticed this is the same logic Kent's using in his answer; I've opted to use the hardcoded assignments to variable `dl` as opposed to the conditional test that Kent uses; same logic (different implementation) and same results ...