Search code examples
ksh

How to reference a file for variables using ksh?


How do I reference a variable from another file using ksh?

I know in bash you just use "source script" and then "echo $variable" but in ksh, when I try that, it does not work.

example I am trying to do:

config.sh:

variable=apple

access_var.sh:

#!/bin/bash
source config.sh
echo $variable

in ksh I just get " config.sh: cannot open [No such file or directory]"


Solution

  • Provide a path for to the file. If the file is in the current directory use source ./config.sh. Otherwise an absolute or relative path is OK, the latter must not start with ./.

    If you don't provide a path ksh searches the file in the directories in $PATH.

    (And remember to change the shebang in the other script to whereever your ksh is (or make sure your scripts are POSIX shell compatible and just use #!/bin/sh to have scripts with a high chance to run everywhere).)