ella
ella

Reputation: 195

read -a doesn't work in unix

I'm trying to split a paramameter into array so that i can then assign into different variables, but somehow i get error saying 'read -a' doesn't work.

command:

bash-4.1$ ./sftpupstream.ksh CheckFile "aaa|bbbb|ccc|dddd"
aaa|bbbb|ccc|dddd
./sftpupstream.ksh[20]: read: -a: unknown option
Usage: read [-ACprsSv] [-d delim] [-u fd] [-t timeout] [-n count] [-N count] [var?prompt] [var ...]

Code:

RUN_MODE=$1
PARAMSTR=$2

echo $PARAMSTR
IFS="|" read -a arr <<< "$PARAMSTR"
for i in "${arr[@]}"; do
        echo "$i"
done

Upvotes: 0

Views: 691

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295272

read -a is not a standardized option to the read builtin. See the POSIX standard for read at http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html, and note that POSIX sh (the baseline specification for all POSIX shells) doesn't even specify arrays at all.

Rather, it's an extension, only available in specific shells. To get the extension, you must be using one of those specific shells. You can do that by:

  • Explicitly starting your script with an extended shell (bash yourscript).
  • Using a shebang indicating an extended shell (#!/bin/bash or #!/usr/bin/env bash as the first line of your script)

If your shell is ksh, the equivalent extension is read -A.

Upvotes: 3

Related Questions