How to Convert a Python Script to a Shell Script

Why might you want to convert your Python script to a Shell script?

Originally, I thought that converting my Python script to a Shell script might make it easier for my crontab to run it, since the main benefit of a Shell script (as far as I can tell) is that it removes dependencies related to directories. Instead of putting a lot of information in your crontab command about changing directories, initializing virtual environments, and running scripts, a Shell command can do it all in one simple command.

So without further ado, here is how you can turn your Python script into a Shell script:

  1. The first step is easy: have a Python script that you want to turn into a Shell script. In my case, it was a small script called “”.
  2. In Sublime, open a new file and copy the following:

    Adjust the last command to be specific to your file.

    Be mindful that the Shell script is initialized in the directory into which you save it. That’s what’s being done by the “pwd” command on the third line; “pwd” is shorthand for the directory in which the file exists. You can test this out by navigating to a directory in Terminal and then running the “pwd” command:

    To be safe, you can always use the absolute directory to your virtual environment and Python script. So, in my case, if I were to save the Shell script into my directory…

    /Users/mnachum/Desktop/how to troubleshoot crontab

    …I could format my Shell script as follows:

    But, just to be safe that the Shell script will continue to work even if I moved it outside of its original directory, I formatted it as follows:

    cd "$(dirname "$0")";
    echo $CWD
    /Users/mnachum/Desktop/how\ to\ troubleshoot\ crontab/hello/bin/python /Users/mnachum/Desktop/how\ to\ troubleshoot\ crontab/
  3. Once the file is all setup, save it with the extension .sh:

  4. Now, in your Terminal application, navigate to the file’s directory and run the command:
    chmod u+x

    In my case, this would be…

    chmod u+x

  5. And that’s it! You now have an executable Shell script! You can run it in Terminal using the following command:


    And, if you were like me and used the absolute virtual environment and Python script directories, then you can move the Shell script outside of its initial directory and still run it. In the example below, I moved the Shell script out of its folder and onto the Desktop:

For additional information, reference this StackOverflow thread.


2 thoughts on “How to Convert a Python Script to a Shell Script

  1. Pingback: How to Leverage Automator to Work the Same Way as Crontab | Moran Nachum

  2. Pingback: How to Troubleshoot Crontab Issues | Moran Nachum

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

%d bloggers like this: