config.sh
hi="/home/user"
date_sec=`date '+%s'`
this is my file1.
main.sh
source config.sh
cd $hi
echo "$date_sec"
The source is not working in main.sh
hi="/home/user"
date_sec=`date '+%s'`
this is my file1.
source config.sh
cd $hi
echo "$date_sec"
The source is not working in main.sh
Some shells requires the file that you source
to be available in your $PATH
unless you give a path to the script:
$ ksh93 main.sh
main.sh[1]: .: config.sh: cannot open [No such file or directory]
Changing the command to
source ./config.sh
should fix this.
Also, .
(dot) is more portable than source
and works with e.g. dash
and should also work with any other shell masquerading as sh
:
. ./config.sh
Apart from that, consider using $( ... )
rather than backticks in new scripts, and remember to quote your variables. I'd also suggest using printf
rather than echo
for variable output.
sh
orbash
or something else? – terdon Apr 26 '17 at 11:11