The error you are getting is likely because
EDITOR is not set - so your command becomes
which tries to execute the
.profile of the invoking user (clearly
root in this case).
Even if set
$EDITOR usually points to an interactive text editor - it's not going to magically take the following line and insert it in the file. In any case
/root/.profile is the personal
.profile file for user
root - no help if you want to modify the system-wide environment.
To make a system-wide change, you should modify either
/etc/environment1 - or (better) create a custom file in the
/etc/profile.d/ directory e.g.
echo 'export MY_VAR="123"' >> /etc/profile.d/myvars
Your script will of course need to be run as root e.g. using
 note that if you use
/etc/environment, remove the
export (variables in this file are just defined as simple