My goal is to initialize multiple bash variables from the output of one single command. Specifically, line i
should be the value of variable i
. Example:
My command is a Python program with the name init.py
:
if __name__ == '__main__':
print("Value of A")
print("Value of B")
print()
print("Value of D")
Desired outcome:
echo "A='$A', B='$B', C='$C', D='$D'"
# --> A='Value of A', B='Value of B', C='', D='Value of D'
What I then tried without success:
read A B C D < <(python init.py)
# --> Effect: A='Value', B='of', C='A', D=''
read -d$'\n' A B C D < <(python init.py)
# --> Effect: A='Value', B='of', C='A', D=''
IFS=$'\n' read A B C D < <(python init.py)
# --> Effect: A='Value of A', B='', C='', D=''
IFS=$'\n' read -d$'\n' A B C D < <(python init.py)
# --> Effect: A='Value of A', B='', C='', D=''
- How to solve this problem?
- How to generalize this to other separators, such as the Null byte
\0
?