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

  • 03-06-2016 09: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: 949 ✭✭✭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