1

I'm working on a simple debian script. What I'm looking for is assigning a string for each line in a text file. For example my text file would be:

abc
def
ghi

I would like a string named 1 to be "abc", string 2 to be "def" and so forth.

I will possibly be working with hundreds of lines in the text file, so if there's a way on auto-naming the strings instead of specifying "string s1 =" etc.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 1
    Your question isn't clear. It is better if you can show the code you have written. In any case, can't you read the lines (strings) from your file into an array instead of having a separate variable for each line? Check this post: [Looping through the content of a file in Bash](https://stackoverflow.com/a/41646525/6862601). Check [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) as well. – codeforester Apr 10 '18 at 20:08
  • Most likely an X-Y problem. What are you going to do with the indexed strings? – karakfa Apr 10 '18 at 20:18

1 Answers1

1

First of all, there's no such thing as a "debian script", there's nothing Debian specific about what you're doing: you're writing a "shell script"

Next, don't create a series of dynamically named variables. That makes your job harder. Use an array.

Since you tag , you will will want to use the mapfile command. At a bash prompt type
help mapfile

Typically, you have

mapfile -t all_lines < filename

Then you have ${all_lines[0]}, ${all_lines[1]}, etc, or

for line in "${all_lines[@]}"; do
    do_something_with "$line"
done

There are tons of questions on this site about working with arrays in bash, do some searching.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352