How getopts is setting a bash variable

281 views Asked by At

I would like to re-implement my own getopts (in python if possible).

My problem is that I don't understand how this is possible:

# The call of the following script
./script -h

#!/bin/bash

getopts 'h' TEST # output nothing
echo $TEST # output h
env | grep # output nothing

We read all over the Internet that you can't set a variable in the parent process. But this is done here and without an export.

I want to understand how it works as I would like to do approximately the same in python. So my second question will be, is it possible to do the same in python?

Thank you very much! :)

1

There are 1 answers

3
Ignacio Vazquez-Abrams On BEST ANSWER

getopts is a bash builtin. Since it operates within the shell process it is welcome to change shell variables at will.