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: Handling lists where items have spaces within

Options
  • 03-06-2016 9:40am
    #1
    Registered Users Posts: 262 ✭✭


    Ive got a dynamically generated list where the user "may" put spaces inside the same element

    e.g

    LIST="'My first thing' 'My second thing'"

    I then iterate and run commands towards it, but this produces

    command My
    command first
    command thing
    etc...

    How can i make the shell smarter for this?! :confused:


Comments

  • Moderators, Computer Games Moderators Posts: 4,281 Mod ✭✭✭✭deconduo


    guylikeme wrote: »
    Ive got a dynamically generated list where the user "may" put spaces inside the same element

    e.g

    LIST="'My first thing' 'My second thing'"

    I then iterate and run commands towards it, but this produces

    command My
    command first
    command thing
    etc...

    How can i make the shell smarter for this?! :confused:

    You can set the field separator ($IFS) to be a newline character instead of a space:

    http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html


  • Registered Users Posts: 869 ✭✭✭moycullen14


    Use arrays

    $ LIST=(A "B C D" E)
    $ for VAR in "${LIST[@]}" ; do echo COMMAND $VAR; done
    COMMAND A
    COMMAND B C D
    COMMAND E
    $


Advertisement