How to change the delimiter when I do a "${myarray[@]}" in Bash?
I have an array of file paths that I want to process with a single command. Some of the filenames have a space. So I can't do this:
echo "${myarray[@]}"|xargs myoperation I'd like to use `xargs -0` but I can't figure out how to make the output of the array delimited by a null character instead of a space. Any ideas? |
Try looking up the IFS variable.
|
IFS doesn't seem to help:
x=("a" "b 2" "c");IFS=$'\0' echo "${x[@]}"|xxd gives me: 0000000: 6120 6220 3220 630a a b 2 c. Maybe I'm just using it wrong? This is in Ubuntu 12 by the way. |
Here's a short example
Code:
a=(a "b 2" c) |
printf can insert the null character after each array element.
Code:
printf '%s\0' "${myarray[@]}" | xargs -0 myoperation |
Quote:
Code:
myoperation "${myarray[@]}" |
Quote:
|
Quote:
x=("a" "b 2" "c");printf '%s\0' "${x[@]}"|xxd gives me: 0000000: 6100 6220 3200 6300 a.b 2.c. |
I agree that printf+xargs is generally the way to go, but another option would be to splice the array with a c-style for loop, to limit the number of entries processed at once.
Code:
for (( i=0; i<${#array[@]} ; i+=50 )); do |
All times are GMT -5. The time now is 05:48 PM. |