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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Bash interpolation

  • 24-04-2009 10:19am
    #1
    Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭


    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, Registered Users 2 Posts: 1,227 ✭✭✭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, Registered Users 2 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
    


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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, Registered Users 2 Posts: 2,755 ✭✭✭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, Registered Users 2 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