Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:04:22+00:00 2026-06-04T06:04:22+00:00

In bash I frequently make scripts where I loop over a list of strings

  • 0

In bash I frequently make scripts where I loop over a list of strings that I define.

e.g.

for a in 1 2 3 4; do echo $a; done

However I would like to define the list (before the loop to keep it clean) so that it contains spaces and with out a separate file:

e.g. (BUT THIS WILL NOT WORK)

read -r VAR <<HERE
list item 1
list item 2
list item 3
...
HERE

for a in $VAR; do echo $a; done

The expected output above (I would like):

list item 1
list item 2
list item 3
etc...

But you will get:

list
item
1

I could use arrays but I would have to index each element in the array (EDIT read answers below as you can append to arrays.. I did not know you could).

How do others declaratively define lists in bash with out using separate files?

Sorry I forgot to mention I want to define the list at the top of the file before the for loop logic

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:04:23+00:00Added an answer on June 4, 2026 at 6:04 am

    Arrays aren’t so hard to use:

    readarray <<HERE
    this is my first line
    this is my second line
    this is my third line
    HERE
    
    # Pre bash-4, you would need to build the array more explicity
    # Just like readarray defaults to MAPFILE, so read defaults to REPLY
    # Tip o' the hat to Dennis Williamson for pointing out that arrays
    # are easily appended to.
    # while read ; do
    #    MAPFILE+=("$REPLY")
    # done
    
    for a in "${MAPFILE[@]}"; do
        echo "$a"
    done
    

    This has the added benefit of allowing each list item to contain spaces, should you have that need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Bash, I would like to create a function that returns the filename of
In bash how can I make a construction like this to work: if (cp
In bash I would like to get filename from a command line string. But
#!/bin/bash for((i=1;i <=10;i++)) do php /var/www/get.php done I have the above shell script that
In bash, sequence numbers e.g. 222R5555 echo {0..9}{0..9}{0..9}{A..Z}{0..9}{0..9}{0..9}{0..9} > seqList.txt Can that line be
I frequently execute from a shell (in my case Bash) commands that I want
In Bash, I would like to use /dev/stdin. As with many commands, stdin is
In bash, calling foo would display any output from that command on the stdout.
In Bash I would like to do an ldap query. It went well but
I frequently write for-loops in bash with the well-known syntax: for i in {1..10}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.