Is there a bash builtin or expression that is equivalent to python's shlex.split? It would be great if it could handle not only whitespace but any IFS.
I have a file with a bunch of simple bash commands.
cat a_filename
ls a\ filename\ with\ spaces
less "another with spaces"
ln -s "this is" my\ favorite
echo $(I do not mind if this does not work)
What I want to do is to process this file line-by-line by splitting the commands on every line into argument arrays. I can do this in python simple enough with shlex.split:
>>> import shlex
>>> for line in open("/tmp/example"):
... print(shlex.split(line))
...
['cat', 'a_filename']
['ls', 'a filename with spaces']
['less', 'another with spaces']
['ln', '-s', 'this is', 'my favorite']
['echo', '$(I', 'do', 'not', 'mind', 'if', 'this', 'does', 'not', 'work)']
Please note that I am not interested in non-bash solutions or solutions that use eval or hand-crafted escaping. The following for example is not a good solution, as it allows command injection.
$ cat /tmp/example_line
cat "this is" not\ ok $(date)
$ eval "array=($(cat /tmp/example_line))"
$ echo ${array[@]}
cat this is not ok Sun Jan 13 17:13:44 CET 2019