Skip to content

Instantly share code, notes, and snippets.

@killwing
Created November 7, 2012 17:44
Show Gist options
  • Save killwing/4033179 to your computer and use it in GitHub Desktop.
Save killwing/4033179 to your computer and use it in GitHub Desktop.
[convertArgsStrToArray] convert args str to array
#!/bin/bash
returnVal=()
convertArgsStrToArray() {
local concat=""
local t=""
returnVal=()
for word in $@; do
local len=`expr "$word" : '.*"'`
[ "$len" -eq 1 ] && concat="true"
if [ "$concat" ]; then
t+=" $word"
else
word=${word#\"}
word=${word%\"}
returnVal+=("$word")
fi
if [ "$concat" -a "$len" -gt 1 ]; then
t=${t# }
t=${t#\"}
t=${t%\"}
returnVal+=("$t")
t=""
concat=""
fi
done
}
# example
lsArgs='-hlF soft "my code" "" "docs" "mystuff" "pic music video"'
convertArgsStrToArray $lsArgs
ls "${returnVal[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment