Score:0

reading multiline output of command into variable in bash script

id flag

I have the following line in a bash script:

disk_info=$(smartctl -i $disk)

where $disk is the name of a disk eg. /dev/sda The output of the command should be many lines long, and I need to further process these as seperate lines, but the variable $disk_info does not contain any newlines, just spaces in their place Example:

echo $disk_info
smartctl 7.1 2019-12-30 r5022 [x86_64-linux-5.15.0-56-generic] (local build) Copyright (C) 2002-19, Bruce Allen, Christian Franke, www.smartmontools.org === START OF INFORMATION SECTION === Model Family: WDC HGST Ultrastar He10 Device Model: WDC WD100EMAZ-00WJTA0 Serial Number: JEJ84B7N LU WWN Device Id: 5 000cca 267dfe25a Firmware Version: 83.H0A83 User Capacity: 10,000,831,348,736 bytes [10.0 TB] Sector Sizes: 512 bytes logical, 4096 bytes physical Rotation Rate: 5400 rpm Form Factor: 3.5 inches Device is: In smartctl database [for details use: -P show] ATA Version is: ACS-2, ATA8-ACS T13/1699-D revision 4 SATA Version is: SATA 3.2, 6.0 Gb/s (current: 6.0 Gb/s) Local Time is: Fri Mar 31 22:06:30 2023 CEST SMART support is: Available - device has SMART capability. SMART support is: Enabled

What am I doing wrong as the output ends up as a single line instead of multiple separate lines, separated by LFs

Edit: Note for people coming across this question in the future: As can be seen from the accepted answer, My initial diagnosis of the problem was way off

Score:1
tm flag

You're not quoting the variable.

echo "$disk_info"

The variable contains all the newlines, but the shell expands the value to parameters to echo. Double quoting the variable prevents the expansion.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.