Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Bash interpolation

Options
  • 24-04-2009 11:19am
    #1
    Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭


    Ok, this question is best explained with an example....

    I want this command:
    for i in [0-9]; do echo $i; done
    

    to produce:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    

    instead, it produces:
    [0-9]
    

    Is there any way (outside a while loop) to achieve this?

    If I do:
    ls *[0-9]*
    

    it is interpolated correctly (i.e. any filename with a number in it is listed).


Comments

  • Registered Users Posts: 1,226 ✭✭✭stereo_steve


    for i in {0..9}; do echo $i; done

    will work, its called a brace expansion if you want to google it.

    I don't know why your command won't work?


  • Registered Users Posts: 868 ✭✭✭brianmc


    Khannie wrote: »
    If I do:
    ls *[0-9]*
    

    it is interpolated correctly (i.e. any filename with a number in it is listed).

    Outside of some other context this is exactly what [0-9] does. i.e. filename matching.

    Doesn't Bash support some class of artihmetic for loop?
    for ((i=0; i<10; i++))
    do
        echo $i
    done
    


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    for i in {0..9}; do echo $i; done

    will work, its called a brace expansion if you want to google it.

    I don't know why your command won't work?

    This is exactly what I was looking for. Cheers. I guess I was used to the [0-9] style from regular expressions and that appears to be what they're used for by bash too.


  • Registered Users Posts: 2,725 ✭✭✭niallb


    Not an answer to your expansion question,
    but a solution to your required output is to use the seq command

    seq 0 1 9
    will give you the number from 0 to 9 in increments of 1.
    If you're going to more digits, -w will zero pad the output,
    so seq -w 0 1 10 will give you 00 to 10 rather than 0 to 10.

    Backticking it will get you closest to your original logic.
    for i in `seq 0 1 9`
    do
    echo $i
    done
    

    The reason your first example doesn't work is that while bash will expand the expression, echo won't.


  • Registered Users Posts: 868 ✭✭✭brianmc


    niallb wrote: »
    The reason your first example doesn't work is that while bash will expand the expression, echo won't.

    You're correct that echo doens't expand anything. It prints out whatever arguments it receives. It has received one argument only though... the "word" "[0-9]". Bash hasn't expanded anything either.

    Given that there's no other context in that line, if there had been filenames in the current directory of 0 or 1 or 2 or any single digit it would have been expanded to the list of those.


  • Advertisement
Advertisement