How to split a string with quotes (like command arguments) in bash?
I have a string like this:
"aString that may haveSpaces IN IT" bar foo "bamboo" "bam boo"
I want to be able to split it like this:
aString that may haveSpaces IN IT
bar
foo
bamboo
bam boo
How do I do that? (preferrably using a one-liner)
When I saw David Postill's answer, I thought "there must be a simpler solution". After some experimenting I found the following works:-
string='"aString that may haveSpaces IN IT" bar foo "bamboo" "bam boo"'
echo $string
eval 'for word in '$string'; do echo $word; done'
This works because eval
expands the line (removing the quotes and expanding string
) before executing the resultant line (which is the in-line answer):
for word in "aString that may haveSpaces IN IT" bar foo "bamboo" "bam boo"; do echo $word; done
An alternative which expands to the same line is:
eval "for word in $string; do echo \$word; done"
Here string
is expanded within the double-quotes, but the $
must be escaped so that word
in not expanded before the line is executed (in the other form the use of single-quotes has the same effect). The results are:-
[~/]$ string='"aString that may haveSpaces IN IT" bar foo "bamboo" "bam boo"'
[~/]$ echo $string
"aString that may haveSpaces IN IT" bar foo "bamboo" "bam boo"
[~/]$ eval 'for word in '$string'; do echo $word; done'
aString that may haveSpaces IN IT
bar
foo
bamboo
bam boo
[~/]$ eval "for word in $string; do echo \$word; done"
aString that may haveSpaces IN IT
bar
foo
bamboo
bam boo
The simplest solution is using making an array of the quoted args which you could then loop over if you like or pass directly to a command.
eval "array=($string)"
for arg in "${array[@]}"; do echo "$arg"; done
p.s. Please comment if you find a simpler way without eval
.
Edit:
Building on @Hubbitus' answer we have a fully sanitized and properly quoted version. Note: this is overkill and will actually leave additional backslashes in double or single quoted sections preceding most punctuation but is invulnerable to attack.
declare -a "array=($( echo "$string" | sed 's/[][`~!@#$%^&*():;<>.,?/\|{}=+-]/\\&/g' ))"
I leave it to the interested reader to modify as they see fit http://ideone.com/FUTHhj