After reading some posts, could not figure out how comes history -d 1 in a function does not delete the entry.

The function below goes directly to the command line:

function test () {
  echo "HISTFILE: '${HISTFILE}'"
  history -d 1

Then we run:

history -c
echo "to remove"
    1  echo "to remove"
    2  history
echo $(test)
HISTFILE: '/home/user/.bash_history'
    1  echo "to remove"
    2  history
    3  echo $(test)
    4  history

Any ideas on how comes it does not remove the first entry of the history?

  • 1
    Why are you running your function in a command substitution as the argument of echo? You don't need to echo its output (it does this just fin by itself already), and the command substitution creates a subshell.
    – Kusalananda
    Commented Oct 4, 2022 at 8:55
  • thanks @Kusalananda... that did it... just test on the command line worked fine.
    – rellampec
    Commented Oct 4, 2022 at 8:58

1 Answer 1


The function (which, by the way, overloads the name of a standard utility, so you should probably change that) is being run within a command substitution, a separate subshell.

Within the subshell, it removes the first history entry. It does not affect the history of the invoking shell, though.

First, rename the function to something other than test (this does not fix the error, but test is a standard utility already), then stop using echo $(some-command) to output the output of a command or function. It's an anti-pattern with several issues. Just use the function's name directly; it is already doing its own output.

Apart from echo and the command substitution being unnecessary, the echo may change the command output if the output contains backslashes. The unquoted command substitution also causes the shell to split the command output on spaces, tabs, and newlines (any character in $IFS), and it would apply filename globbing to the split-up words. The echo utility would then output all generated words with spaces between them on a single line.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .