An Entrepreneur, Coach, IT Consultant, Strategic Adviser, and a Traveler craving to explore and contribute to forming a better society.

Tuesday, August 25, 2009

Shell Scripting #0016: setenv and unsetenv in C Shell

No comments :
setenv and unsetenv in C Shell

In the C shell, variables are exported if you set them with "setenv", but not it you set them with "set". Thus, if you want your shell variable modifications to be seen by any tool or script that you call, you should use the "setenv" builtin. This "setenv" builtin is the C shell equivalent to issuing an assignment statement with the "export" builtin in the Bourne shell.

setenv VAR "Value"
echo "VAR is '$VAR'."

If you want your shell variables to only be available to your script, you should use the "set" builtin. The "set" builtin is equivalent to a simple assignment statement in the Bourne shell.

set VAR "Value"
echo "VAR is '$VAR'."

To remove variables in the C shell, you can use the "unsetenv" or "unset" builtin.

If you need to test an environment variable (not a shell-local variable) that may or may not be part of your environment (a variable set by whatever process called your script), you can use the "printenv" builtin. This prints the value of a variable if set.

set X = `printenv VALUE`
echo "X is $X"

No comments :