You want:
#!/bin/bash
su - mv2 <<'EOSU'
DELIVER_BRANCH="development"
echo "$DELIVER_BRANCH"
exit;
EOSU
Note the single quotes around the first EOSU
.
If you omit them, the heredoc undergoes $
-interpolation before being passed, which means that "$DELIVER_BRANCH"
gets replaced with the current (to the shell invoking su
) content of $DELIVERY_BRANCH
, which is empty:
DELIVERY_BRANCH=production
cat <<'EOSU'
DELIVER_BRANCH="development"
echo "$DELIVER_BRANCH"
exit;
EOSU
prints
DELIVER_BRANCH="development"
echo "$DELIVER_BRANCH"
exit;
whereas
DELIVERY_BRANCH=production
cat <<EOSU
DELIVER_BRANCH="development"
echo "$DELIVER_BRANCH"
exit;
EOSU
prints
DELIVER_BRANCH="development"
echo production
exit;
su - USER -c 'echo XXX'
– Rabin yesterdaysu
does not work well when piping commands in. Maybe stdin is not used or used in some other way (could not find something in the man page). But as @Rabin said you can pass short shell scripts via the-c
option:su -c 'DELIVER=foo; echo $DELIVER; command1; command2 ...'
– Lucas yesterday