Question

I am trying to run the following script but the value of the variable $user does not get printed. Can someone please explain?

#!/usr/bin/ksh -x

x=`tty`
at -k now <<end
        user=Bill
        echo $user >> $x
end
Was it helpful?

Solution

You cannot set a variable inside heredoc. You can do this:

x=`tty`
user=Bill

at -k now <<end
   echo $user >> $x
end

OTHER TIPS

The problem is that variables are expanded in here docs when the end token is unquoted.

This means that the command that you pass to at is:

user=Bill
echo >> /dev/pts/4

If you quote the end token, i.e.

at -k now << 'end'
        user=Bill
        echo $user >> $x
end

then what you're passing to at is

user=Bill
echo $user >> $x

In this case $user is set in and expanded by at's invoked shell, and will expand as you expected.

Note in this case that $x has to be exported, since otherwise at won't see it, won't include it in the environment of the invoked shell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top