0

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

closed as unclear what you're asking by tripleee, Toby Speight, Jeff Schaller, Michael Durrant, DarkHeart Apr 26 '17 at 13:22

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    What language are you using? Is it sh or bash or something else? – terdon Apr 26 '17 at 11:11
  • ... and what is the error message? – Kusalananda Apr 26 '17 at 11:12
  • Possible duplicate of sudo source: command not found – tripleee Apr 26 '17 at 11:18
  • 1
    Welcome to Unix & Linux! It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please edit your question to give a more complete description of what you expected to happen and how that differs from the actual results. See How to Ask for hints on what makes a good explanation. – Toby Speight Apr 26 '17 at 11:45
2

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.