-1

A nice user solved my problem, so I changed my question, now its not the same as before the other one was. My permutation look like this now:

x=3
for i in $(eval echo "{1..$x}{1..$x}{1..$x}"); do
echo "$i"
done

How can I get just these numbers (123,132,231,213,321,312) from the output with two "for" periods and with "seq" command?

hategrip
  • 29
  • 5
  • Your output makes no sense, it doesn't appear to follow any pattern. – 123 Jun 07 '16 at 13:45
  • 2
    Your first "permutation" prints only empty lines: `i` is not `X`. – Benjamin W. Jun 07 '16 at 13:53
  • I didn't even need `eval`. Try `input=2; for i in {1..$input}{1..$input}{1..$input} ; do echo $i; done`. Good luck. – shellter Jun 07 '16 at 13:57
  • oh sorry, I wanted to write x. – hategrip Jun 07 '16 at 13:58
  • thank you very very much! And what if I want to filter them and get only those, which arent contain the same number? I know I should use "for" and "seq", to get it somehow. – hategrip Jun 07 '16 at 14:01
  • 1
    @shellter In bash braces are expanded before variables. You might have zsh as your shell? – Andreas Louv Jun 07 '16 at 14:01
  • @shellter You cant use variables in brace expansion as braces are evaluated first. – 123 Jun 07 '16 at 14:02
  • @andlrc (and @123) . It really works for me, but I'm using ksh93. Forgot about one of the corner cases where `bash` and `ksh` work differently. Thanks for the reminder! – shellter Jun 07 '16 at 14:06
  • 1
    Possible duplicate of [How to make permutation in bash with N! input?](http://stackoverflow.com/questions/37639939/how-to-make-permutation-in-bash-with-n-input) – Andreas Louv Jun 07 '16 at 14:14
  • I will delete that topic. Sorry for that. edit: I cant.. – hategrip Jun 07 '16 at 14:17
  • Please don't change your question, especially if the old question already has answers. Ask a new question, instead. – mob Jun 07 '16 at 20:41

1 Answers1

1

if you want to be able to dynamically set 3 in {1..3} something like {1..$x}. If you try that you will experience that the output will be {1..3} and not the expected 1 2 3. This is because braces are expanded before variables.

What you need to use is eval echo "{1..$x}" which will indeed output 1 2 3. And in a for loop you could use a command substitution:

x=3
for i in $(eval echo "{1..$x}{1..$x}{1..$x}"); do
  echo "$i"
done
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • can you please help me how to filter them? I know there are much better version of this, but Id like to do it like this. The output without the same numbers like: 111, 112, etc. I have to do it with two for periods in each other.. and have to use seq command. (sorry for my bad english, Im trying..:D) – hategrip Jun 07 '16 at 14:07
  • @hategrip So you want consider 123 and 321 as the same? – Andreas Louv Jun 07 '16 at 14:09
  • no, I dont. I want to have just: 123, 132, 231, 213, 312, 321. :) ! but I must have to do it with this beginning, which you have written for me. – hategrip Jun 07 '16 at 14:10
  • @hategrip You asked this question two days ago? Why ask it again? – Andreas Louv Jun 07 '16 at 14:14
  • Because people didnt understand me, but know I understand my question(!), so i started again. – hategrip Jun 07 '16 at 14:17
  • do you have any ideas how to do it like this? – hategrip Jun 07 '16 at 14:47