arcad31a
arcad31a

Reputation: 1

How to assign the total number of lines in a file to the size variable for an array in a bash script?

i've been trying to get something like this to work for a while now but I keep running into little imperfection that mess up building my array.

#!/bin/bash

There are a total of four lines in this repo file

repofile=~/Home/Documents/repoKali

I type this into the command line

wc -l < $repofile

I get the following output

6

But when I type this

SIZE=$(wc -l < "$repofile")

I get this

6: command not found

I'm trying build an array that is as big as the number of lines in $repofile. I'm not sure why the commands work outside of variable assignment and not when I assign them to SIZE. I mean the output changes! or am I just missing something?

Please Help. I'm trying to do something like this.

readarray -s $SIZE < $repofile

Upvotes: 0

Views: 97

Answers (1)

glenn jackman
glenn jackman

Reputation: 246807

You don't need to initialize bash arrays with a size, just put values into the array.

The -s option for readarray is not for "size", it's for "skip":

Options: ...

-s count   Discard the first COUNT lines read

From a bash prompt, type help readarray for all the details.


This error 6: command not found indicates to me that you're putting a space after the = sign: no spaces are allowed for variable assignment

SIZE= $(wc -l < "$repofile")
#....^

Upvotes: 2

Related Questions