I'm trying to run a script which switches users (following this answer). I'm unable to set a variable within this. I've tried many things but the most basic is:
sudo -u other_user bash << EOF
V=test
echo "${V}"
EOF
More realistically I am doing something similar to the following:
sudo -u other_user bash << EOF
cd
V=$(ls)
echo "${V}"
EOF
Every time I try to use the variable V
it is unset. How can I set a variable?