Поскольку не было ответов, которые бы помогли мне, я решил поделиться сценарием, который я сейчас использую.Это, конечно, не одна строка и даже не bash ... но это работает.
#!/usr/bin/env python
import os
user=os.getenv('USER')
if not user:
user='hobs'
home=os.getenv('HOME')
if not home:
home=os.path.normpath(os.path.join(os.path.sep+'home',user))
histfile=os.getenv('HISTFILE')
if not histfile:
histfile=os.path.join(home,'.bash_history')
from optparse import OptionParser
p = OptionParser(usage="%prog [options] password", add_help_option=True)
p.add_option('-p', '--password', '--pass', '--pw', dest='pw',
default =None,
help="The plaintext password string you'd like to find and replace throughout your bash history file(s)", )
p.add_option('-r', '--replacement-password', '--replacement', '--filler', '--substitution', dest='rep',
default ='',
help="The replacement string, passphrase identifier, tag, or filler you'd like to leave behind wherever the password was found and removed.", )
p.add_option('-f', '--bash_history', '--historyfile', '--file', '--path', '--filename', dest='hfile',
default =histfile,
help="The text file where your password may have been accidentally recorded and where you'd like it removed. Default = ~/.bash_history.", )
(o, a) = p.parse_args()
if a and not o.pw:
o.pw=' '.join(a) # password can have spaces in it
print o.pw
print len(o.pw)
# TODO: Check if the history buffer will record the invocation of this script that includes a plaintext password
# Alternatively, launch the search/replace task in the background to start
# after history has had a chance to record the last command in the history buffer
if o.pw:
import warnings
warnings.warn("Make sure you invoked "+p.get_prog_name()+" in such a way that history won't record this command (with a plaintext password) in the history file. It would be much better if you didn't supply the password on the command line and instead allowed this script to securely prompt you for it.",RuntimeWarning)
if not o.pw:
import getpass
o.pw=getpass.getpass()
if len(o.pw)<4:
raise ValueError(p.get_prog_name() + " doesn't accept passwords shorter than 4 characters long to prevent accidental corruption of files by purging common character combinations. The password you supplied is only "+str(len(o.pw))+" characters long.")
import fileinput
for line in fileinput.FileInput(o.hfile,inplace=1):
line = line.replace(o.pw,o.rep)
print line,