Pass a bash variable to python script

The python equivalent of the shell's positional parameter array $1, $2 etc. is sys.argv

So:

#!/usr/bin/env python

import sys

def getPermutation(s, prefix=''):
        if len(s) == 0:
                print prefix
        for i in range(len(s)):
                getPermutation(s[0:i]+s[i+1:len(s)],prefix+s[i] )



getPermutation(sys.argv[1],'')

then

$ ./foo.py abcd
abcd
abdc
acbd
acdb
adbc
adcb
bacd
badc
bcad
bcda
bdac
bdca
cabd
cadb
cbad
cbda
cdab
cdba
dabc
dacb
dbac
dbca
dcab
dcba

Lots of ways to parameterize python. positional args, env variables, and named args. Env variables:

import os and use the getenv like:

fw_main_width  =os.getenv('FW_MAIN_WIDTH',  fw_main_width)  

Where the second parameter is the default for the env variable not being set.

Positional args:

Use the sys.argc, sys.argv[n] after you import sys.

Named parameters:

Or for named parameters,(what you asked)

 import argparse  

then describe the possible parameters:

parser = argparse.ArgumentParser(description = "Project", fromfile_prefix_chars='@')
parser.add_argument("-V", "--version", help="show program version", action="store_true")
parser.add_argument("-W", "--width", help="set main screen width")  
read arguments from the command line  

args = parser.parse_args()

and use them as args.width etc.


Okay, I figured out a workaround while I was writing the question but I felt that this would useful to other users so here it is.

For python (python2), we can use raw_input() instead of $1 but it works a bit differently. Instead of entering the input after the script name in bash, you are prompted to input the value after you run the script.

Here is an example:

#!/usr/bin/env python
def getPermutation(s, prefix=''):
        if len(s) == 0:
                print prefix
        for i in range(len(s)):
                getPermutation(s[0:i]+s[i+1:len(s)],prefix+s[i] )



getPermutation(raw_input("enter characters: "),'')

Running the script will prompt the user to "enter characters:". After the user enters the characters and then presses ENTER, the permutations will print in the terminal.

Here is the source which also explains how to do this for python3.