Im trying to source a large script with many functions in it that take some time. I would like to present a dialog "sourcing please wait..." in the mean time. once done sourcing i want to use my functions. but using the dialog seems to cancel the souring. the sourcing does processed with the dialog but the function does not run.

file: main.script.sh


long task...

function PrintDate(){

file: job1.sh

source main.script.sh | dialog --progressbox "sourcing please wait..." 20 80

command prompt:

bash job1.sh

output i get

PrintDate: command not found

Nit picks:

  1. Your “shebangs” are wrong — they should begin #!.
  2. You don’t actually need a “shebang” in a file that is meant to be sourced.  You might want to leave it off so it’s clear that the file is meant to be sourced and not run as an ordinary script.  (Specifically, you should probably start it with a comment that says something like
    # This file is meant to be sourced and not run as an ordinary script.)
  3. Strictly speaking, you don’t need a “shebang” in a file that is meant to be run by bash scriptname, although it doesn’t hurt.
  4. Of course you’re getting PrintDate: command not found.  Your main.script.sh file isn’t defining PrintDate; it’s defining PritnDate.

But the real answer to your question is:

  1. The whole point of source is that the sourced script gets read and interpreted in the current main shell process, but when you make it part of a pipeline, you force it to run in a child process.
  2. The trick is to create a pipe without creating a pipeline.  The trick to doing this is using process substitution (a bash extension to command substitution).  This command:

    source main.script.sh > >(dialog --progressbox "sourcing please wait..." 20 80)

    will create a child process to run the dialog program, and create a pipe from the main shell process to that child process, but then it makes that pipe look like an ordinary file, and it sends the output from the source command to the pipe while making it look like an ordinary redirection of the standard output.  Therefore, the source runs in the main shell process (which, in this case, is the bash process that’s running the job1.sh script), and so your functions are defined in the job1.sh script.

| improve this answer | |
  • ive corrected according to your remarks. its was just a typo from my side and thanks that works for me! – Asaf Magen Feb 27 '17 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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