David542
David542

Reputation: 110267

Get shell input as string, not list

If I have the command:

$ /file.py item 2

Doing sys.argv would give me:

['/file.py', 'item 2']

Is there a method to get the exact text inputted, without doing ' '.join(sys.argv) ?

Upvotes: 1

Views: 57

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295500

The exact end-user input given is never communicated from the shell to the program being run. Assembling an argument vector is performed by the shell, and that vector -- and not the string from which it is built -- is passed as an argument to the execve system call.

Indeed, there may not exist a shell command at all -- think of the case where your script is invoked with subprocess.call(['/file.py', 'item 2'], shell=False), or its equivalents in other languages.

Without modifying your shell to do something special (such as exporting the last command to an environment variable -- something which could be easily implemented with a DEBUG trap), there is no possible way to retrieve it.

Upvotes: 4

Related Questions