0

I'm trying to seperate the ip and port using a bash script. Below is what I have

my proxies.txt file

48.54.87.45:1000
48.54.87.45:1001
48.54.87.45:1002

My bash script

#!/bin/sh
input="proxies.txt"
while IFS= read -r var
do
  echo "$var"
  IFS=':' read proxy port <<< "$var"
  echo "$proxy"
  echo "$port"
  echo "---------------"
done < "$input"

But I get the following error

Syntax error: redirection unexpected

I have tested the loop and it does read the file one by one. It's the IFS line which is giving the error.

What have I done wrong?

2
  • Make sure your shell is bash, not /bin/sh. Commented Apr 13, 2018 at 19:36
  • And btw, make it while IFS=: read -r proxy port _ and you'll only need the one read. Or if you are going to do a full-line read, it's cheaper in terms of performance to do proxy=${var%%:*} and port=${port#*:} Commented Apr 13, 2018 at 19:36

1 Answer 1

1

<<< is bash-only syntax. If you try to use it in a script run with /bin/sh (with either a #!/bin/sh shebang or run with sh scriptname), it will cause this error.

Start your script with #!/usr/bin/env bash, or another shebang that actually invokes bash.

Sign up to request clarification or add additional context in comments.

2 Comments

I already have that. I will edit my question
You edited the question to show /bin/sh. sh is the wrong shell. bash is the right one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.