2

I'm using a blackbox CLI based on Bash and I'm not entirely sure what stuff I can use. Brace expansion doesn't work, and with it goes my ability to do loops without listing the arguments explicitly, which is something I was trying to avoid by looping to start with.

for x in {1..5}
do
    for y in {a..c}
    do
    echo $HOME$x$y
    done
done

How do I run something like this without brace expansion and without listing the arguments explicitly? Environment variables should also work, that's why I appended a random $HOME to the example.

Please feel free to provide different alternatives (AWK, sed) as I'm not entirely sure what will and what won't work.

3
  • 1
    Do you have access to the jot command?
    – Fox
    Commented Mar 12, 2019 at 1:13
  • 3
    So this appears to be not based on Bash based on your description; what makes you think that it is? Is there any further detail available about the system, the shell that's in it, or the tools that are available there? Commented Mar 12, 2019 at 2:02
  • @MichaelHomer It was an assumption made based on other things not relevant here. I do not claim to have been a reasonable assumption, but it was one I did. I was wrong. Thank you very much, this prompted me to make sure and find out the truth. Commented Mar 12, 2019 at 9:57

2 Answers 2

0

You'd better use jot as commented. Here is a (not so pretty) awk implementation.

#!/usr/bin/awk -f
BEGIN{
    for(i=1;i<5;i++)
        for(j=97;j<100;j++)
            printf "%s%d%c ",ENVIRON["HOME"],i,j;                               
    print;
}

or (you may also use the -v option of awk to assign shell variables to awk variables)

#!/bin/bash
awk -f /dev/fd/3 3<< EOF                                                                                                                                                                                    
BEGIN{
    for(i=1;i<5;i++)
        for(j=97;j<100;j++)
            printf "$HOME%d%c ",i,j;
    print;
}
EOF
0

If you're sure you run bash, maybe brace expansion isn't enabled.

Check it with by using

echo $-
himBHs

In the shell I'm using, the option B is set, indicating that brace expansion is enabled.

You can set the brace expansion by using one of these commands:

set -o braceexpand
set -B

In this case I don't need to loop over the letter or numbers:

echo {1..5}{a..c}
1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c

or disable it by using either

set +o braceexpand
set +B

leading to

echo {1..5}{a..c}
{1..5}{a..c}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .