0

Is there a way to view/show/print the source code of bash script with its includes (aka "sources")?

For example:

sub.sh file

 function showMe(){
     echo "INCLUDE"
    }

main.sh file

#!/bin/bash    

source sub.sh

showMe    
echo "OK"

The "print" of main.sh will show:

#!/bin/bash    

showMe(){
 echo "INCLUDE"
}

showMe    
echo "OK"
  • 1
    source code can be seen by more/less/cat, at run time you can use -x flag (e.g. set -x ). There is no such a magic that will statically print print source code, and included file. No equivalent to cpp for C language. – Archemar Jul 21 '15 at 13:57
  • How do you "print" of main.sh ? – Pandya Jul 21 '15 at 14:17
0

You can try following awk:

awk '/^source/ { while (getline l <$2 > 0) print l; close($2); next; } { print; }' main.sh

so each line which starts with source fname should be replaced with contents of file if exists.

1
perl -p0e 'while(s/source\s+(\S+)/`cat $1`/e){}' foo.sh
  • Works great.. but I can't use perl, thankyou by the way. – xlive Jul 21 '15 at 14:13
  • put echo "source foo.sh for the rescue" in foo.sh and you get an infinity loop:) – Evgeny Vereshchagin Jul 22 '15 at 6:55
  • @EvgenyVereshchagin, you are very cruel! :) – JJoao Jul 22 '15 at 8:55
1

If you don't mind executing the script too, here is a simple way to have the source code and its includes being displayed:

bash -v foo.sh
  • This command also parse the script, I need only the source, thankyou. – xlive Jul 21 '15 at 14:29

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.