Viesturs
Viesturs

Reputation: 123

Make inputs from console expandable

Assume this short shell script

#! /bin/sh                                                                      
read -p "Enter directory: " dir
echo $dir

or python script

#!/usr/bin/env python                                                           
dir = input("Enter directory: ")
print dir

When I try to input the directory from the console using environmental variables, e.g. $PROJECT

$PROJECT/src

the environmental variable is not expanded but TAB appears as a TAB instead.

Is there a way to make inputs from console expandable?

Upvotes: 0

Views: 59

Answers (1)

ndrwnaguib
ndrwnaguib

Reputation: 6135

Why do not use sys.argv?You can pass the environmental variable as a parameter to your script. For example; assume the following script:

#!/usr/bin/env python           
import sys                                                

print sys.argv

When we run it as the following:

python2 example.py $PROJECT_PATH

You will find the $PROJECT_PATH as the second element in sys.argv list. It will exactly look like the following output when running the above command:

['example.py', '/path/to/example/project']

Upvotes: 2

Related Questions