putenv command not working

Dear all,

I am trying to use the shell putenv command to define some environmental variables. I used the following syntax:

variable narm index 2

shell putenv narm=${narm}

However, I find that the variable ‘narm’ has not been defined in the environment. When I enter ‘echo $narm’ in the terminal, a blank line is returned. Kindly advise.

Best,
Karthik

***DISCLAIMER*** The sender of this email is an alumnus of National University of Singapore (NUS). Kindly note that NUS is not responsible for the contents of this email, and views and opinions expressed are solely the sender's.

Dear all,

I am trying to use the shell putenv command to define some environmental variables. I used the following syntax:

variable narm index 2

shell putenv narm=${narm}

However, I find that the variable ‘narm’ has not been defined in the environment. When I enter ‘echo $narm’ in the terminal, a blank line is returned. Kindly advise.

what you are seeing is correct behavior. you can change the environment for the LAMMPS executable, but not for the shell that has executed it.
you will see the same behavior, if you type (on the command line):

bash
export narm=2
echo $narm

exit
echo $narm

you will see the first echo will report the expected value, as you are on the same “level”, i.e. the process of the extra bash executable. once you exit this process, the original settings are retained.

axel.

Dear Sir,

Thank you for the quick response. There is one more thing I would like to check. Suppose I want to modify a particular line in a text file called ‘input.dat’. I tried to do the following:

shell sed -i "1s/.*/$n1 $n2/" input.dat

where the index style variables n1 and n2 have been previously defined in the lammps input script. This gives the following error:

sed: -e expression #1, char 7: unterminated `s' command

However, if I only attempt to write a single variable using the command ‘shell sed -i "1s/.*/$n1/" input.dat’, everything works fine. May I know why this is the case? What can I do if I want to replace a particular line in the middle of a text file with the values of multiple variables? I am aware of the print and fix print commands but those seem to either overwrite files or append data to their end.

Best,
Karthik

Dear Sir,

Thank you for the quick response. There is one more thing I would like to check. Suppose I want to modify a particular line in a text file called ‘input.dat’. I tried to do the following:

shell sed -i “1s/.*/$n1 $n2/” input.dat

where the index style variables n1 and n2 have been previously defined in the lammps input script. This gives the following error:

sed: -e expression #1, char 7: unterminated `s’ command

for the text in quotation marks, no immediate variable expansion happens. when it is passed as an argument to sed, the double quotation marks are stripped off, hence the error about the unterminated ‘s’ command. the command line passed to the subshell would have been:

sed -i 1s/.*/$n1 $n2/ input.dat

to have the command you expect to happen multiple things have to be done. for immediate expansion of variables names with more than 1 letter you have to use curly braces, i.e. {n1} or {n2}, or else you get an error of the kind “Illegal substitution for variable n” (unless you by accident also have a variable n defined).
also, you must not use quotation marks. so what should work is this:

shell sed -i 1/.*/{n1}\ {n2}/ input.dat

However, if I only attempt to write a single variable using the command ‘shell sed -i “1s/.*/$n1/” input.dat’, everything works fine. May I know why this is the case?

this latter command cannot have worked correctly. see my explanation above.

What can I do if I want to replace a particular line in the middle of a text file with the values of multiple variables? I am aware of the print and fix print commands but those seem to either overwrite files or append data to their end.

you haven’t looked at the rules for variable expansion and input file processing careful enough. it is a bit complex, but all the information is there. since this is custom written code, you must not assume, that it is the same as in, say, bourne shell or similar. some features are, but not all.

axel.