0

The Issue

I have a Python script that when I run it from the command line I do not want to record anything within .bash_history.

The reason for this is that the script uses the Python argparse library which allows me to pass in arguments to the python code directly from the command line.

For example I could write the script so that it would use "123456" as a value in the script:

$ ./scriptname.py -n 123456

The issue is that I don't want the value 123456 stored in .bash_history. In fact, I'd rather the entire command was never stored into the .bash_history file in the first place.

What I've Tried

  1. Subprocess & history -c

I've added the subprocess library to the top of my script and then included this directly after to attempt to proactively clear the current history of the shell I am working in:

subprocess.call("history -c", shell=True)

Theoretically this should clear the history of the current shell. I don't see errors from this so I'm assuming that it runs in some other shell. When I run it outside of the script (directly after running the command to invoke the script) it works properly.

  1. Subprocess & unset HISTFILE

I have also used subprocess with the following with no success:

subprocess.call("unset HISTFILE", shell=True)
  1. os.system & history -c

I've also used the os library for Python and included the following in the script:

os.system("history -c")
  1. os.system and unset HISTFILE

I've also tried unset HISTFILE with os.system to no avail.

os.system("unset HISTFILE")

Preferred Solution Characteristics

I realize that I could simply type in unset HISTFILE or history -c after using the command. But I want this to be as much as possible a self-contained script.

Ideally the solution would prevent the ./scomescript.py command from ever being recorded within .bash_history.

I need this script to output text to the terminal based on the input so I can't close the terminal immediately afterwards either.

I imagine there must be a way to do this from within the python script itself - this is my preference.

Fernando
  • 429
  • 6
  • 16
  • I heard somewhere that if you put a space before a bash command, it won't be saved. That doesn't work for me, but that's what I've heard. – zondo Mar 20 '16 at 04:24
  • Thanks zondo, unfortunately I think that requires a special environment variable setup. See: http://stackoverflow.com/questions/8473121/execute-command-without-keeping-it-in-history – Fernando Mar 20 '16 at 04:26
  • Well, thanks for clearing that up. It's a shame that the person who actually asked a question isn't getting an answer, but I am ;/ – zondo Mar 20 '16 at 04:28
  • Is this because the argument is a secret, for example a password? – rmarston Mar 20 '16 at 07:05

1 Answers1

1

This really isn't very feasible... Adding the entry to the history file is performed by the interactive shell, and it occurs after the command has completed and the parent shell exits. It is, strictly speaking, possible, if you were to make your python program execute spawn a hacky background process that did something like read the history file in a loop re-writing it. I really can't advocate anything like this, but you could append your script with something like:

os.system("nohup bash -ic 'while :; do read -d \"\" history < \"$HISTFILE\"; echo \"$history\" | sed -e\"s@^%s.*@@\" -e\"/^$/d\" > \"$HISTFILE\"; sleep 1; done &' >/dev/null 2>&1" % sys.argv[0])

I think a much better way to accomplish your goal of not recording any arguments would be to use something like var = raw_input("") instead of passing sensitive argument on the command line.

You could also perhaps create a shell function to wrap your script, something like my_script(){ set +o history; python_script.py "$@; set -o history ;}?

Geoff Nixon
  • 4,697
  • 2
  • 28
  • 34
  • History is recorded after the shell closes not after the command completes. In my testing I verified this. Therefore if my script was able to properly run unset HISTORY in the same shell shouldn't this work in the way I intend? Thanks for the alternative solution though. I will look into using that as a backup. – Fernando Mar 20 '16 at 15:30
  • Sorry, you're correct about the history _file_, I meant to say its added to _history_ after the command is completed; I'll edit the answer. Ultimately, the issue is that a subprocess cannot make persistent changes to the parent shell's environment; the only way to modify the history file from within your python program would be with a background task. I'll add a couple examples of what I mean for completeness' sake. – Geoff Nixon Mar 21 '16 at 04:16
  • Ahhh. Damn. I see what you're saying. Theoretically the history -c would work except for the fact that I can't make any changes in the parent shell at all. Oh well. Foiled. I'll try one of your alternatives. Thanks Geoff. – Fernando Mar 21 '16 at 13:51