0

I'm trying to run a simple shell script example:

STR="qwe;ert;rty;tii"
IFS=';' read -r NAMES <<< "$STR"

This is giving me the following error message:

syntax error: got <&, expecting Word

I'm not exactly sure why this isn't working. I thought the syntax I used was correct and I tried comparing to other examples and I saw the syntax was almost identical.

Any feedback would help

Thanks

20
  • 1
    There is no <& is your code. Please make sure that your snippet actually reproduces your problem before asking a question. Commented Aug 1, 2016 at 16:35
  • 2
    ...btw, we had this exact question posted, and deleted, a few days ago: stackoverflow.com/questions/38667961/shell-script-issue Commented Aug 1, 2016 at 16:37
  • 1
    What's your operating system and exact shell version? Is this "Git Bash" or some other MSYS-based Windows build? What does echo "$BASH_VERSION" emit? If you save this script to a file, and run bash -x YourFileNameHere, what gets emitted? Commented Aug 1, 2016 at 16:46
  • 1
    ...as another aside, it's best practice to avoid all-uppercase variable names, which are used by variables with meaning to the shell and operating system. We get too many questions from folks who used PATH as a name for one of their own variables and then wonder why nothing else works; using path (str, names, etc) instead avoids collisions. Commented Aug 1, 2016 at 16:49
  • 1
    9.4.1 isn't a version of bash that exists -- the most recent release series is 4.3.x as of this writing. Presumably you're using something which isn't, well, actually bash. Commented Aug 1, 2016 at 16:49

1 Answer 1

1

This is MKS bash, not GNU bash. It's not really bash, and doesn't support the genuine shell's syntax.

There are perfectly good (...well, reasonably adequate) builds of GNU bash for Windows. Use them.


Particularly, in real bash, to split a semicolon-separated string into an array of names:

str='qwe;ert;rty;tii'
IFS=';' read -r -a names <<<"$str"

...which you can then verify with

declare -p names

or emit one-to-a-line with

printf '%s\n' "${names[@]}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.