Search code examples
arraysbashcsvcut

Syntax Error in BASH script - readarray + cut command


I am trying to parse a comma separated file to a couple arrays.

First array will contain values from the 1st column of the file Second array will contain values from the 3rd column of the file

I am using readarry and cut to try to accomplish this task, but I am getiing a syntax error:

readarray -t VLANs < <( cut -d, -f1 /logs/repository/vlansList.csv )
readarray -t VLANs < <(cut -d, -f1 $1)

None of the 2 approaches above are working.

Error is:

./ios2NX.sh: line 59: syntax error near unexpected token `<'
./ios2NX.sh: line 59: `         readarray -t VLANs < <( cut -d, -f1 /logs/repository/vlansList.csv)'

Funny thing is that the error is not displayed if I test the script with bash - [script_name]

I tried putting the path in quotes and double quotes, the same error was thrown.

Do you guys know what I am missing here or can suggest a better approach than the readarray + cut ? (lines in my file contain white spaces)

Thanks in advance!


Solution

  • The problem is that you're writing bash but running with sh.

    If your shebang is #!/bin/sh, replace it with #!/bin/bash

    If you're running sh yourfile, instead run bash yourfile.