I have some commands in my Bash history that I want to remove.
I can find them with history | grep "command with password"
then remove them with history -d <line-number>
However, when I try to delete them in bulk by piping them to xargs
like this I get an error:
history | grep "command with password" | awk '{print $1}' | sort -r | xargs history -d
xargs: history: No such file or directory
I thought that xargs
would go through the list of line numbers and send it one by one to the history -d
command.
What is causing this error?
NB: I know there are other ways to delete the history, the question is purely to improve my understanding of how xargs
works and what I am doing wrong that is causing the error.
Best Answer
The error is raised because xargs cannot find a
history
command. It is a shell builtin, as you can confirm withtype history
, thus not visible to xargs. TryThe return value is 127. In
man xargs
, EXIT STATUS section:Expanding on ilkkachu's comment, in principle you could invoke Bash and call
history
from the spawned shell.The above command does not raise an error, after all the builtin
history
is available to the Bash shell. However, from that same command one also finds thatHISTFILE
is unset andhistory
outputs nothing: The history is not enabled on a non-interactive shell. Again, you could activate it with theset
builtin, exportHISTFILE
andHISTFILESIZE
... But we don't want a headache. Editing.bash_history
directly is the straightforward way to go.