user294015 Asked: 2018-04-17 03:39:05 +0800 CST2018-04-17 03:39:05 +0800 CST 2018-04-17 03:39:05 +0800 CST IFS to read systeminfos 772 I tried to read some system infos and put them into variables: df -k | grep /dev/mmcblk0p1 | IFS=" " read -r device blocks used available use_percent mounted_on It did not work, all variables are empty. bash ifs 2 Answers Voted steeldriver 2018-04-17T04:00:01+08:002018-04-17T04:00:01+08:00 The issue is not the IFS, it's because in bash (and certain other shells) the RHS of a pipeline is executed in a subshell. A simple alternative is to use process substitution to keep the read in the parent shell: read -r device blocks used available use_percent mounted_on < <(df -k | grep /dev/mmcblk0p1) See Bash: Variable assignment doesn't seem to 'stick' oliv 2018-04-17T04:05:40+08:002018-04-17T04:05:40+08:00 If you assign variables using read, these must be part of the same block statement { ... }: df -k | grep /dev/mmcblk0p1 | { read -r device blocks used available use_percent mounted_on printf "device=%s\nblocks=%s\nused=%s\navailable=%s\nuse_percent=%s\nmounted_on=%s\n" $device $blocks $used $available $use_percent $mounted_on }
The issue is not the IFS, it's because in bash (and certain other shells) the RHS of a pipeline is executed in a subshell.
A simple alternative is to use process substitution to keep the
read
in the parent shell:See Bash: Variable assignment doesn't seem to 'stick'
If you assign variables using
read
, these must be part of the same block statement{ ... }
: