Embedding Ruby code

You can run Ruby code from a bash shell script. In this case, you will have one bash script and a Ruby script. You can simply call the Ruby script from within the Bash shell script.

If you want to combine both scripts in one script, then we will need to use the << heredoc functionality of Bash shell.

Let's write the embed_03.sh script:

#!/usr/bin/env sh echo "This is bashScript!" /usr/bin/env rubyScript <<-EndOfRuby puts 'This is ruby!' EndOfRuby

Let's test the program:

    $ chmod +x embed_03.sh
    $ ./ embed_03.sh
  

The output is as follows:

    This is bashScript! This is rubyScript!
  

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.