I want to attach a string to each of the elements in an array in bash, but not if the array is empty.
Currently this works for non-empty arrays:
$ cat this.sh
#!/bin/bash
lst=$@
lst[0]="a"
lst[1]="b"
echo ${lst[@]/%/.ext}
$ bash this.sh
a.ext b.ext
But it fails for an empty array:
$cat that.sh
#!/bin/bash
lst=$@
echo ${lst[@]/%/.ext}
$ bash that.sh
.ext
So this fails, as I want the string to be empty, if the array is empty.
Restriction: I can't use an if-statement or loop constructs in this particular situation.
if? eg[ ${#lst} -gt 0 ] && echo ${lst[@]/%/.ext}Incidentally, yourlstvariable isn't initially an array, just a string containing all the positional parameters.