Inspect Arguments with Your Favorite Programming Langauges
Insert any of the following commands between
xargs and commands you want to execute.
ruby -e 'p ARGV'
node -e 'console.log(process.argv)'
python -c 'import sys; print sys.argv'
In this answer, I would use
ruby -e 'p ARGV' for every example.
Let's say you want to debug the script:
echo 1 2 3 4 | xargs echo
To debug, put
ruby -e 'p ARGV' before
echo 1 2 3 4 | xargs ruby -e 'p ARGV' echo
["echo", "1", "2", "3", "4"]
As we can see, it's very clear that
echo received 2 arguments.
Here is another example using
echo 1 2 3 4 | xargs -I@ ruby -e 'p ARGV' echo @
["echo", "1 2 3 4"]
Now we know
echo received only one argument.
xargs -t -p?
-p are really ambiguous when there are white spaces in command arguments, for example:
printf 'hello world\0goodbye world' | xargs -0 -t echo
echo hello world goodbye world
hello world goodbye world
When looking at
echo hello world goodbye world, it's hard to tell whether
echo received 2 or 4 arguments.
By using the solution, it's easy to understand how
xargs treats each arguments:
printf 'hello world\0goodbye world' | xargs -0 ruby -e 'p ARGV' echo
["echo", "hello world", "goodbye world"]