Timeline for Why do I get an error when deleting entries from Bash history?
Current License: CC BY-SA 4.0
17 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 20, 2020 at 14:15 | comment | added | fzbd | @ilkkachu I guess I wasn't clear, but by "current shell session" I meant the one started by xargs, of course it will be distinct from the shell session that starts xargs. My point still stands that history operations are possible to do under xargs, and it is suitable for the purpose of understanding the behaviour of interactive vs non-interactive shells regarding bash builtins. | |
| Jul 20, 2020 at 12:12 | comment | added | Quasímodo |
@ilkkachu Thank you for your important remarks. Particularly: "If the command is in the history of a running shell (only), it only gets written to $HISTFILE when the shell exits". In which case the reader should refer to Chris Down's answer in the linked question.
|
|
| Jul 20, 2020 at 11:51 | comment | added | fzbd |
@ilkkachu "preferable" depends on the user's intended task. If they just want to modify the current shell session history, my comment shows how to access it in xargs, which was one of the points from OP (i.e. "improve my understanding of how xargs works"). If they want to remove it persistently, they edit the file instead of using the history builtin. Hope that clears any confusion.
|
|
| Jul 20, 2020 at 11:08 | comment | added | ilkkachu | @fzbd, and just to repeat the point I made above until it sticks, that doesn't work for removing anything from the history file. It's not just "preferable" to edit the file itself, or to do something other than that, it's necessary. | |
| Jul 20, 2020 at 10:13 | comment | added | fzbd |
For those wondering, history can work under xargs by forcing an interactive shell and setting the history option in bash: echo 1 | xargs -i bash -ci 'set -o history; echo $HISTFILE; history' _ {}. But as others pointed out, it may be preferable to edit the file itself.
|
|
| Jul 20, 2020 at 9:30 | comment | added | ilkkachu |
"Editing .bash_history directly is the straightforward way to go." -- well, except that if the command is in the history of a running shell (only), it only gets written to $HISTFILE when the shell exits, so it might not be there to be removed yet. Though if the shell has read some history from $HISTFILE, and you then remove the lines from the file, at least the shell doesn't seem to write them back. But if you have something already in $HISTFILE, and use history -d , it doesn't seem to delete it from the file. So, what you want to do depends on where the command is already stored.
|
|
| Jul 20, 2020 at 7:47 | comment | added | ilkkachu |
@Quasímodo, it would probably use /bin/sh. (or on the more weird systems, some other path where it knows a POSIX shell is available; if /bin/sh isn't one.) Or perhaps what's in the user's SHELL, possibly.
|
|
| Jul 20, 2020 at 4:53 | comment | added | Peter Cordes |
@opticyclic: xargs isn't a shell-builtin itself so it runs in a separate process. If you wanted to do something like it, you might write a shell loop that used while read, or bash array variables and iterate over them, or some other pure shell construct that can run commands inside the current shell.
|
|
| Jul 20, 2020 at 0:23 | history | edited | Quasímodo | CC BY-SA 4.0 |
Added links, expanded on invoking a shell
|
| S Jul 20, 2020 at 0:00 | history | suggested | user339704 | CC BY-SA 4.0 |
changed the typography a bit
|
| Jul 19, 2020 at 21:39 | review | Suggested edits | |||
| S Jul 20, 2020 at 0:00 | |||||
| Jul 19, 2020 at 21:08 | comment | added | opticyclic | Both comments totally make sense. Very useful in helping my understanding! | |
| Jul 19, 2020 at 20:57 | comment | added | Quasímodo |
@opticyclic It is a bit surprising at the first moment, but if you think about it, how would xargs decide on which shell to look for builtins? bash, ksh, dash, zsh? It seems reasonable that it doesn't go as far as that.
|
|
| Jul 19, 2020 at 20:49 | comment | added | ilkkachu |
@opticyclic, it could, if it used a shell to start the programs, instead of doing it itself. And you can do it anyway with something like xargs sh -c 'some shell code' sh. It just wouldn't help because that copy of the shell might have a different concept of the history than the main shell where you started the whole pipeline
|
|
| Jul 19, 2020 at 20:33 | comment | added | opticyclic | Very interesting. I never knew that xargs couldn't call shell builtins! | |
| Jul 19, 2020 at 20:32 | vote | accept | opticyclic | ||
| Jul 19, 2020 at 20:24 | history | answered | Quasímodo | CC BY-SA 4.0 |