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: 
You could pipe the for-loop output to `head -c-1` to delete the trailing comma:

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

```
Another one is using read and PE directly inside the command substituion. 

```
export foo=$(read -r var < <(for i in {0..7}; do printf '%s' "uops_dispatched_port.port_$i|p$i,"; done); printf '%s' "${var%,}" )

```

Or use the good ol pipe with command grouping using braces `{ }`

```
export foo=$(for i in {0..7}; do printf '%s' "uops_dispatched_port.port_$i|p$i,"; done | { read -r var ; printf '%s' "${var%,}" ; } )

```