BASH Shell Scripting: Passing Arguments to a Python Script

Larry Wall: “Shell programming is a 1950s juke box …”

In this short blog I write a small Python utility script to create directories and demonstrate how to utilize it and pass it arguments via BASH shell script. The functionality is similar to the ‘mkdir’ UNIX command. The name of the directory or directories to be created is the argument I pass in to the Python script. I then create a BASH shell script to call the Python script.

The BASH shell script file takes in a argument(s) and passes the argument(s) to the Python script file. I make the BASH script file an executable with the “chmod +x [BASH script name]” command; I can then put this small utility script in one my path directories as shown by the “echo $PATH” command and call it anytime as needed from the shell.

Python script code (mkd.py):

Python 'mkd.py' script code

Python 'mkd.py' script code

BASH shell script code (mkd):

BASH shell 'mkd' script code

BASH shell 'mkd' script code

Follow me on Twitter: @Humair_Ahmed

This entry was posted in BASH Shell Scripting, Mac OS X Mavericks, Operating Systems, Programming Languages, Python, Technology and tagged , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

1 Response to BASH Shell Scripting: Passing Arguments to a Python Script

  1. Orfeu65 says:

    Hi!
    Thanks for the script. I had problems when trying to pass an argument with double-quotes with spaces inside. I had tryed with “$*” and other options, but it was only with the “$@” it worked.
    Great!

Leave a Reply

Your email address will not be published. Required fields are marked *


3 − = zero