1

In the simplest form I am puzzled why this script does not work as expected as a here script changing users with su - user <<EOF.

su - db2prd<<EOF
PARMDATE=1111111
echo parmdate
echo $PARMDATE
EOF

produces output:

parmdate

and removing the su/EOF part:

PARMDATE=1111111
echo parmdate
echo $PARMDATE

produces:

parmdate
1111111

I am unable to figure out why ksh variable assignment is not working and how to make it work, presuming the environment wasn't set, etc. I have tried everything I can search on. Variable assignment seems broken in the su - <<EOF part.

This is just an example from a much larger script that switches users for some maintenance activities.

1

Variables are interpreted in a here doc (<<...), so you need to escape the ones you don't want evaluated yet.

su - db2prd<<EOF
PARMDATE=1111111
echo parmdate
echo \$PARMDATE
EOF

Or better, quote the delimiter (here EOF) to tell your shell not to perform expansions inside the here-document:

su - db2prd<<'EOF'
PARMDATE=1111111
echo parmdate
echo $PARMDATE
EOF
  • 1
    Rather than us \$ every time you refer to a variable, you can say su - db2prd << \EOF. – G-Man Mar 4 '15 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.