Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Bash interpolation

  • 24-04-2009 11: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,231 ✭✭✭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,780 ✭✭✭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