Read character and use as separator

Hi all,

I'm trying to read in a character and use it as a separater on a string:

#!/bin/ksh

echo "Enter input line"
read input_header

echo "Enter separator:"
read separator

IFS="$separator" read -A fields <<< "$input_header"

for ((i=0;i<${#fields[@]};i++)) ; do
  echo ${fields[$i]}
done

This works when using characters like comma's etc. But not with spaces and tabs:

-bash-4.1$ ./test.ksh
Enter input line
test1,test2,test3
Enter separator:
,
test1
test2
test3
-bash-4.1$ ./test.ksh
Enter input line
test1 test2 test3
Enter separator:
   <- space
test1 test2 test3

Is there any way to do this?

Thanks

Try IFS="" read -r separator

1 Like

Thanks! Works great

1 Like

" " (space) is one of the default IFS chars and thus is removed during read . An alternative to corona688's modifying the IFS is escaping the space in the input line:

./shscr
Enter input line
test1 test2 test3
Enter separator:
\                             # 1 escaped space here
test1
test2
test3
1 Like