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

  • 03-06-2016 8:40am
    #1
    Registered Users, Registered Users 2 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,282 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, Registered Users 2 Posts: 895 ✭✭✭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