Skip to Content
Learning Linux Shell Scripting - Second Edition
book

Learning Linux Shell Scripting - Second Edition

by Ganesh Sanjiv Naik
May 2018
Beginner
332 pages
7h 28m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting - Second Edition

Embedding Python code in Bash shell Script

Nowadays, Python is a popular scripting language, especially in data science and automation. You can integrate Python code very easily in bash scripts. We have used here doc for this purpose.

Let's write the shell script embed_01.sh as follows:

#!/bin/bash 
function now_date_time 
{ 
python - <<START 
import datetime 
value = datetime.datetime.now() 
print (value) 
START 
}  
now_date_time 
Date_Time=$(now_date_time) 
echo "Date and time now = $Date_Time"

Let's test the program as follows:

    $ chmod +x embed_01.sh
    $ ./ embed_01.sh
  

The output is as follows:

    2018-04-27 01:53:58.719905
    Date and time now = 2018-04-27 01:53:58.770123
  

Let's see an example of using bash variables in embedded Python code.

Let's write ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting

Learning Linux Shell Scripting

Ganesh Sanjiv Naik
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 9781788993197Supplemental Content