-1

I'm trying to make a loop in Bash. I tested the loop with for adr in a{a,b,c}d and it works perfectly with aad, abd, acd

But if I have a variable with the value a{a,b,c}d, it doesn't return the above values; instead it runs only once with the value a{a,b,c}d.
How do I enable this?

Alternatively, I'm trying to take an input from the user, and then replace all instances of ~const~ with {b,c,d,f,g...y,z} and then run the loop I described above.

Unfortunately, as I said, it isn't working. Is there any oher way to make this work?

Yash Sharma
  • 31
  • 10
  • See: [how to use variables with brace expansion](https://stackoverflow.com/q/33491233/3776858) – Cyrus Jul 03 '17 at 19:29

2 Answers2

1

This code is a roundabout way of doing what you want. It outputs everything on a new line:

test=( a b c ) #declare the array
for index in "${test[@]}"; do #uses for loop to go through each item in the array
echo "{a{$index}d}" | tr -d '{}'; #echoes the result without curly braces
done

Hope this was what you were looking for!

  • It's almost what I want. Only difference, the 'a' and the 'd' are also meant to be variables that the user enters. In fact, the user will input a~const~d, and I want to display abd, acd, add, afd ... – Yash Sharma Jul 04 '17 at 10:01
  • You dont need to echo `{ }` and then remove them with `tr`. Just don't echo those brackets at all: `echo "a${index}d"` or in case of variables `echo "${start}${index}${end}"` where `start="a"` and `end="d"` – George Vasiliou Jul 04 '17 at 21:18
  • I know this is somewhat older, but you could edit my answer.. Instead of "test=( a b c )", you could have "test=( $a $b $c )." – Nythepegasus Aug 15 '17 at 15:33
0

These are some workarounds:

$ aa=( a{a,b,c}d );for adr in ${aa[@]};do echo "$adr";done #using array
aad
abd
acd

$ aa=$(echo a{a,b,c}d);for adr in ${aa};do echo "$adr";done #without array
aad
abd
acd

This is how above can work with variables:

$ start="a"
$ var1="a"
$ var2="b"
$ var3="c"
$ end="d"
$ aa=$(echo $start{${var1},${var2},${var3}}$end)
$ for adr in $aa;do echo "$adr";done 
aad
abd
acd
George Vasiliou
  • 6,130
  • 2
  • 20
  • 27
  • You're right, I accidentally added an additional loop. Removing now. – Yash Sharma Jul 04 '17 at 07:30
  • I tried both of what you suggested. They work perfectly in the terminal, but when I try to implement in the code, the value of $adr comes out to a{b,c,d}d, even within the loop. Is this because I'm taking the value of $aa as an input? – Yash Sharma Jul 04 '17 at 07:49
  • @15KanurajBeri See update in my answer on how you can make this work with variables. – George Vasiliou Jul 04 '17 at 11:53
  • Thank you. This is very helpful. But, if the user just enters "abc~const~", with no ending variable, then how will we split it? – Yash Sharma Jul 04 '17 at 11:58
  • @15KanurajBeri If you unset (or not set at all) the last variable `$end` in my example , will work as expected. Splitting input text to three texts and apply brace expansion in the middle text, is a whole new exercise. Moreover you can take a look to parameter expansion `${parameter:+word}` (see man bash for details - https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion) – George Vasiliou Jul 04 '17 at 12:25