How can I make an environment variable available outside of a KSH script?

0

I have a KSH script that exports an environment variable (export SOME_VAR=123)

After running the script my current shell is unaffected and echo $SOME_VAR produces nothing.

I tried running . myScript.ksh but got the following error:

.: Permission denied.

Permissions for . are drwxr-xr-x

Any idea? (I'm not root)

RonK

Posted 2011-08-29T13:17:18.193

Reputation: 1 280

What permissions does myScript.ksh have? Is it readable? (Using . name is the correct way, since a process cannot update its parent's environment, which is why running the script "as a script" did not work.) – user1686 – 2011-08-29T13:18:54.553

The script's permissions are -rwxr-xr-x - and it runs when invoking it without . before it.

Can it be related to the fact my shell is TCSH? – RonK – 2011-08-29T13:29:36.827

Yes, it could. (See answer below.) – user1686 – 2011-08-29T13:34:38.163

Answers

3

You are using tcsh as your shell:

  1. tcsh does not have a . command – only source.

    > source myScript.ksh
    

    In sh, ksh and bash shells, "." is a built-in command, unrelated to the "current directory" usage of "."

    In csh and tcsh, no such built-in exists (the equivalent is named "source"), and using . will attempt to execute a directory, hence the "Permission denied" error.

  2. tcsh is a csh derivative, and uses a very different syntax for setting environment variables:

    setenv SOME_VAR 123
    

    This matters because using ./source causes the file's contents be executed in the current shell, meaning they have to be valid tcsh syntax.

user1686

Posted 2011-08-29T13:17:18.193

Reputation: 283 655

Thank you - so to summarize, I cannot do what I wanted with a KSH script executed in a TCSH shell, right? – RonK – 2011-08-29T14:31:44.247

@RonK: Right. If you want to change enviroment variables of your current shell, you have to use . or source, and for that you must use a script written specifically for your current shell. – user1686 – 2011-08-29T16:10:22.933

@RonK: There is another possibility — write a script, in any language that outputs (echo's) the apropriate setenv lines, then tell tcsh to evaluate that script's output: eval \./myscript``; this is somewhat fragile, though — you have to be very careful with output quoting. – user1686 – 2011-08-29T16:12:57.640