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 can use the parameter expansion to remove the trailing comma eg.:

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

```

Below is the fragment from [the Bash manual](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) that describes this construct:

> 
> ${parameter%word}  
> 
>  ${parameter%%word} 
> 
> 
> The word is expanded to produce a
>  pattern and matched according to the rules described below (see
>  [Pattern Matching](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html#Pattern-Matching)). If the pattern matches a trailing portion of the
>  expanded value of parameter, then the result of the expansion is the
>  value of parameter with the shortest matching pattern (the ‘%’ case)
>  or the longest matching pattern (the ‘%%’ case) deleted. If parameter
>  is ‘@’ or ‘\*’, the pattern removal operation is applied to each
>  positional parameter in turn, and the expansion is the resultant list.
>  If parameter is an array variable subscripted with ‘@’ or ‘\*’, the
>  pattern removal operation is applied to each member of the array in
>  turn, and the expansion is the resultant list.
> 
> 
>
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)

```