I have a black list to save tag id list, e.g. 1-3,7-9
, actually it represents 1,2,3,7,8,9
. And could expand it by below shell
for i in {1..3,7..9}; do for j in {$i}; do echo -n "$j,"; done; done
1,2,3,7,8,9
but first I should convert -
to ..
echo -n "1-3,7-9" | sed 's/-/../g'
1..3,7..9
then put it into for
expression as a parameter
echo -n "1-3,7-9" | sed 's/-/../g' | xargs -I @ for i in {@}; do for j in {$i}; do echo -n "$j,"; done; done
zsh: parse error near `do'
echo -n "1-3,7-9" | sed 's/-/../g' | xargs -I @ echo @
1..3,7..9
but for
expression cannot parse it correctly, why is so?
Because you didn't do anything to stop the outermost shell from picking up the special keywords and characters (
do
,for
,$
, etc ) that you mean to be run byxargs
.xargs
isn't a shell built-in; it gets the command line you want it to run for each element on stdin, from its arguments. just like any other program, if you want;
or any other sequence special to be bash in an argument, you need to somehow escape it.It seems like what you really want here, in my mind, is to invoke in a subshell a command ( your nested
for
loops ) for each input element.I've come up with this; it seems to to the job:
which gives: