-3

I'm trying to split a line with multiple ' in it, but the result are numbers, not strings, trying to get the 1st occurrence of ', I mean the string YouTube down there:

line="application: label='YouTube' icon='res/uFR.xml'"
echo $line
IFS="'"
read -a stringArray <<< "$line"
echo "${stringArray[1]}"

Current Output:

112

Expected Output:

YouTube

https://i.sstatic.net/VYcIh.jpg

6
  • 1
    Hi, please read minimal reproducible example and update your question accordingly. Commented Apr 11, 2021 at 3:59
  • 1
    I can't tell if it's the problem here, but setting IFS globally like this can cause all sorts of weird problems, especially when you're also using word splitting (which depends on IFS) to get the items the for loop iterates over (which also isn't very safe on its own, either). It's better to make it a prefix of the read command (IFS=\' read -a ...) so that it only applies to that one command. Commented Apr 11, 2021 at 4:05
  • @Nic3500 How's now? Commented Apr 11, 2021 at 14:37
  • the latest edit (line=.. / echo $line / IFS=... / read -a ... / echo ...) works for me and generates as output the string YouTube; and the array contents look correct: typeset -p stringArray => declare -a stringArray=([0]="application: label=" [1]="YouTube" [2]=" icon=" [3]="res/uFR.xml") Commented Apr 11, 2021 at 18:30
  • please update the question what a textual snippet of the code that shows up in the imgur image, making sure to specify that you're running this script under adb Commented Apr 11, 2021 at 18:53

2 Answers 2

0

Can you believe this guys ?! It should be capital A, not small a, must be Bash magic :)

read -A stringArray <<< "$line"
Sign up to request clarification or add additional context in comments.

1 Comment

I'd like to note here that Linux files must've combatible line ending to be read correctly: Notepad++ -> Edit -> EOL Conversion -> Unix (LF), or you can just Console.Write("string" + "\n");
-1

i think the IFS variable should use quotes for it to be literal, if this does not work then to also escape the quote character with IFS='\''

1 Comment

Thanks, but this is not the case, it works fine in Ubuntu like the screenshot in the main post says.

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.