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

Madura
  • 11
  • 1
  • 1
  • 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](https://unix.stackexchange.com/questions/143858/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 [ask] for hints on what makes a good explanation. – Toby Speight Apr 26 '17 at 11:45

1 Answers1

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.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936