0

My idea is to break some value to the seperated variable. Below is example that script failed to get output as needed. Any suggestion for the idea?

A=12-13-14
IFS="-" read -p I J K <<<$A

echo Lin number is $I
echo Michele Number is $J
echo Gina number is $K

my expected output is :

Lin number is 12
Michele number is 13
Gina number is 14
  • <<foo is a here-doc. You want a here-string: <<<. And you gave no prompt text after -p. – muru Oct 21 at 7:59
  • i put string <<< and get below output. still didn't get as expected: Lin number is <br/> Michele Number is 12 13 14 <br/> Gina number is <br/> – ruslyislam Oct 21 at 8:02
  • @ruslyislam edit your question to show the exact command you used. Don't put commands and their output in comments, the formatting just doesn't work – ilkkachu Oct 21 at 8:06
  • "And you gave no prompt text after -p" – muru Oct 21 at 8:22
  • can you explain more what is prompt text you mean? – ruslyislam Oct 21 at 8:32
1

You seem to possibly be using bash since you're using a "here-string" (<<<). Additionally, in some other shells, read -p will read from a coprocess, which means your code would produce different results from what you say in comments. So it's most likely bash.

The -p option to the read utility in bash takes an option-argument. This option-argument is a string that will be used as the interactive prompt when asking a user for a value. In your code, that prompt will be the string I, while the variables J and K will be assigned data by read from standard input (your string $A). This is why $I is an empty string when you echo it.

$ read -p 'Enter value: ' thing
Enter value: hello
$ printf '%s\n' "$thing"
hello

I assume you meant to use something like -r instead of -p:

A=12-13-14
IFS='-' read -r I J K <<<"$A"

printf 'Lin number is %s\n'     "$I"
printf 'Michele Number is %s\n' "$J"
printf 'Gina number is %s\n'    "$K"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.