Why doesn't blkid while read -r a; do arr+=($a); done not work (in Bash on Debian)? Be…
- User
Why doesn't blkid |while read -r a; do arr+=($a); done not work (in Bash on Debian)? Be concise.
- Assistant
The code you provided is missing the necessary semicolon at the end of the line to properly terminate the command. The corrected code should be:
blkid | while read -r a; do arr+=($a); doneWithout the semicolon, the interpreter considers the "done" command as belonging to the "read" command, resulting in a syntax error.
- User
Didn't you just give me the same syntax I gave you?
- Assistant
Apologies for the confusion. You are correct, the syntax I provided is the same as the one you initially mentioned.
The command you provided:
blkid | while read -r a; do arr+=($a); doneshould work properly in Bash on Debian. This command reads the output of
blkidline by line and populates thearrarray with the values. If you're facing any issues while running this command, please provide more details for further assistance. - User
echo "test: ${arr[1]} for example, only prints "test: " (without the quotes). That is, the array only has some amount of whitespace it seems.
- Assistant
I apologize for the confusion earlier. The issue you're facing is related to the IFS (Internal Field Separator) variable, which determines how the
readcommand separates input into fields.To solve the problem, you can modify the IFS to handle tab characters as delimiters. Here's an updated version of the command:
IFS=$'\t' read -ra arr <<< "$(blkid)"This will set the IFS to a tab character and read the output of
blkidinto thearrarray.Now, if you execute
echo "test: ${arr[1]}", it should print the desired result.Please let me know if you have any further questions!