Bash – How to suppress the space between generated arguments during brace expansion

bashbrace-expansionzsh

I used the following command to create a list of URLs that I want to test:

echo -e localhost:8080/reports/{promos,promo-updates,scandown}/{130,139,142}{,-unburdened,-burdened}{,.pdf,.xls,.xlsx,.csv,.preload}"\n" >> urls.txt

Unfortunately the URLs appended to urls.txt each had a space before them (except for the first, of course). I understand why that happened, and I realize I could just strip it off by piping throug a sed expression, but I'd like to know if there's a way to suppress it instead. (It may seem silly and pedantic, but it's no different than the preference so many people feel for not "abusing cats".)

I tried double-quoting to suppress word-splitting, but that suppressed the brace expansion too, so that was a no-go.

I tried changing IFS to an empty string, but it didn't work either:

IFS='' echo -e localhost:8080/reports/{promos,promo-updates,scandown}/{130,139,142}{,-unburdened,-burdened}{,.pdf,.xls,.xlsx,.csv,.preload}"\n" >> urls.txt

Nor did changing it to a newline:

IFS='\n' echo -e localhost:8080/reports/{promos,promo-updates,scandown}/{130,139,142}{,-unburdened,-burdened}{,.pdf,.xls,.xlsx,.csv,.preload}"\n" >> urls.txt

Best Answer

You could store the brace expansion in an array, then output it in the manner of your choosing:

urls=( localhost:8080/reports/{promos,promo-updates,scandown}/{130,139,142}{,-unburdened,-burdened}{,.pdf,.xls,.xlsx,.csv,.preload} )

Then

printf "%s\n" "${urls[@]}"

or

(IFS=$'\n'; echo "${urls[*]}")

The echo example looks weird because:

  1. it's run in a subshell (the parentheses) so I don't alter my current value of IFS.
  2. IFS needs to be defined in a separate command:
    • This doesn't work: IFS=$'\n' echo "${urls[*]}" because the variable gets expanded before the new env variable takes effect
    • IFS needs to be set before you start expanding variables.

Also, note the subtle difference in the dereferencing array index used:

  • [@] in the printf example to expand the array into individual words
  • [*] in the echo example to expand the array into a single word, with elements separated by the first char of IFS
Related Question