{"id":57964,"date":"2024-01-30T07:21:38","date_gmt":"2024-01-30T07:21:38","guid":{"rendered":"https:\/\/www.askpython.com\/?p=57964"},"modified":"2025-04-10T20:42:13","modified_gmt":"2025-04-10T20:42:13","slug":"python-scripts-with-bash","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/python-scripts-with-bash","title":{"rendered":"Python and Bash Integration in Linux: A Step-by-Step Guide"},"content":{"rendered":"\n<p>Bash scripts are powerful tools for automating tasks in the Unix\/Linux environment. Combining the flexibility of Bash with the capabilities of Python allows you to create robust and efficient scripts. In this article, we will explore the process of calling Python script from bash with the example.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>To call a Python script from Bash in Linux, first create the script (e.g., python_script.py). Use a text editor like vi or nano. The Python script should import sys for command-line arguments. Then, create a Bash script (e.g., run_pyscript.sh) with &#8216;#!\/bin\/bash&#8217; and a line to execute the Python script with arguments. Make the Bash script executable using &#8216;chmod +x&#8217; and run it with &#8216;.\/run_pyscript.sh arg1 arg2&#8217;.<\/em><\/p>\n<\/blockquote>\n\n\n\n<p><em><strong>Also read<\/strong><\/em><a href=\"https:\/\/www.askpython.com\/python\/examples\/uninstall-python-3-7-ubuntu\" data-type=\"post\" data-id=\"57302\"><strong><em>: How to Uninstall Python 3.7 from Ubuntu<\/em><\/strong><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Call a Python Script from Bash<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, you have to create a Python script. Here, we create a Python script named &#8216;python_script.py&#8217;.<\/li>\n<\/ul>\n\n\n\n<p>You can create a Python script by using any of the text editors\u2014for example, nano, vi.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nvi python_script.py\n<\/pre><\/div>\n\n\n<p>This command opens the vi text editor.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nnano python_script.py\n<\/pre><\/div>\n\n\n<p>Alternatively, use this command for the nano text editor.<\/p>\n\n\n\n<p>Here we are simply creating a Python script using the vi text editor.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"532\" height=\"30\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/vi_command.png\" alt=\"Vi Command\" class=\"wp-image-57966\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/vi_command.png 532w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/vi_command-300x17.png 300w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li> Now we are creating a simple Python script.  Press <strong>&#8216;i&#8217;<\/strong> and now you are in the insert mode. Now you can start typing your Python code.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport sys\nnum1 = float(sys.argv&#x5B;1])\nnum2 = float(sys.argv&#x5B;2])\n\nresult = num1 + num2 \nprint(f&quot;The sum is: {result}&quot;)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"506\" height=\"212\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/python_add.png\" alt=\"Python Add\" class=\"wp-image-57965\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/python_add.png 506w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/python_add-300x126.png 300w\" sizes=\"auto, (max-width: 506px) 100vw, 506px\" \/><\/figure>\n\n\n\n<p>After entering your code, press<strong> &#8216;Esc&#8217;<\/strong> to exit insert mode. To save and exit type <strong>&#8216;:wq!&#8217;<\/strong> and press <strong>&#8216;Enter&#8217;<\/strong>. This writes the changes to the file and quits the editor. <\/p>\n\n\n\n<p>&#8216;<strong>import sys<\/strong>&#8216; this import <strong>sys<\/strong> module, which provides access to some variables used by the interpreter and functions that interact with the interpreter. &#8216;<strong>sys.argv<\/strong>&#8216;, contains command line arguments passed to the script. &#8216;<strong>sys.argv[]<\/strong>&#8216;,  by using this the subsequent elements are the arguments provided when running the script. &#8216;<strong>float<\/strong>&#8216;, converts the value into floating point number and assign it to the respective variable as num1 and num2. The last line simply prints the sum.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We are creating a Bash script that calls the Python script and passes arguments. <\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nvi run_pyscript.sh\n<\/pre><\/div>\n\n\n<p>Here we create a bash script using vi named run_pyscript.sh<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!bin\/bash\n\npython3 python_script.py &quot;$1&quot; &quot;$2&quot;\n<\/pre><\/div>\n\n\n<p>We create a bash script named run_pyscript.sh and pass the argument as python3 followed by the Python file name.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"482\" height=\"115\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/bash_script.png\" alt=\"Bash Script\" class=\"wp-image-57968\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/bash_script.png 482w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/bash_script-300x72.png 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/figure>\n\n\n\n<p>&#8216;<strong>python3 <\/strong>&#8216;calls the python3 interpreter. &#8216;<strong>python_script.py<\/strong>&#8216; is the name of the Python script to be executed.  You can replace this with the actual name of your Python script. <strong>&#8220;$1&#8221;<\/strong> and <strong>&#8220;$2&#8221;<\/strong> are positional arguments or command-line arguments passed to the Bash script and represent the first and second arguments respectively. <\/p>\n\n\n\n<p>Now press<strong> &#8216;Esc&#8217;<\/strong> to exit insert mode. To save and exit type <strong>&#8216;:wq!&#8217;<\/strong> and press <strong>&#8216;Enter&#8217;<\/strong>. This writes the changes to the file and quits the editor. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>So before executing the Bash script, we have to make the Bash script executable. To make it executable you have to use the following command: <\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nchmod +x run_pyscript.sh\n<\/pre><\/div>\n\n\n<p>This command is used in the Unix\/Linux operating system. &#8216;<strong>chmod<\/strong>&#8216; is used to change mode, it is used to change the permissions of a file. &#8216;<strong>+x<\/strong>&#8216;, adds the execute permission to the file. &#8216;<strong>run_pyscript.sh<\/strong>&#8216; is the name of the Bash script file for which we want to change the permission. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now you can run the bash script. Execute the Bash script to call the Python script with arguments by using the following command:<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n.\/run_pyscript.sh arg1 arg2\n<\/pre><\/div>\n\n\n<p>Replace arg1 and arg2 with the desired values you want to pass as arguments to your Python script.<br>Here in this example, we pass values as 2 and 3.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"530\" height=\"52\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/run_bash_command.png\" alt=\"Run Bash Command\" class=\"wp-image-57969\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/run_bash_command.png 530w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/12\/run_bash_command-300x29.png 300w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/figure>\n\n\n\n<p>&#8216;<strong>.\/run_pyscript.sh<\/strong>&#8216; executes the Bash script with values 2 and 3. Gives output as the sum of 2 and 3 which is 5. Similarly, you can pass arguments to a Python script from the command line.<\/p>\n\n\n\n<p><em><strong>Also read: <a href=\"https:\/\/www.askpython.com\/python\/examples\/run-python-script-terminal-mac\" data-type=\"post\" data-id=\"52871\">macOS \u2013 How To Run Python Script On The Terminal?<\/a><\/strong><\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Here we call a Python script from a bash script in Linux. First, we created a Python script using vi editor and then similarly created a bash script. After that, we make a bash script executable and then we run the bash script to call the python script with arguments. This technique is useful for integrating Python functionality into Bash scripts. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">References:<\/h4>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/4377109\/shell-script-execute-a-python-program-from-within-a-shell-script\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/4377109\/shell-script-execute-a-python-program-from-within-a-shell-script<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash scripts are powerful tools for automating tasks in the Unix\/Linux environment. Combining the flexibility of Bash with the capabilities of Python allows you to create robust and efficient scripts. In this article, we will explore the process of calling Python script from bash with the example. To call a Python script from Bash in [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":64037,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-57964","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=57964"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57964\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/64037"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=57964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=57964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=57964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}