Modify bash history from inside subshell in an interactive sessionAutomatic cleanup of Bash historyBash $PROMPT_COMMAND messing up scroll history displayHow can I programmatically add entries to Bash history and have timestamps work properly?Commands run when terminal is open do not appear in historyBash Executing Script from within Script Causes Echo and Read IssuesCross-session duplicate removal in bash command historyHow can I use vi to edit prompt line of a utility?Bash script order mixup in large redirect of outputshopt -s extdebug in .bashrc not working in script filesWhy is bash history substitution still enabled by default?

How much cash can I safely carry into the USA and avoid civil forfeiture?

Cayley's Matrix Notation

Multiple fireplaces in an apartment building?

Find a stone which is not the lightest one

Multiple options vs single option UI

Find the identical rows in a matrix

What does "function" actually mean in music?

Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?

What is the term for a person whose job is to place products on shelves in stores?

Does a large simulator bay have standard public address announcements?

Why is the underscore command _ useful?

How bug prioritization works in agile projects vs non agile

Unknown code in script

Can a Bard use the Spell Glyph option of the Glyph of Warding spell and cast a known spell into the glyph?

Is there a word for the censored part of a video?

How exactly does Hawking radiation decrease the mass of black holes?

How to be good at coming up with counter example in Topology

Magical attacks and overcoming damage resistance

Why must Chinese maps be obfuscated?

"The cow" OR "a cow" OR "cows" in this context

Why do real positive eigenvalues result in an unstable system? What about eigenvalues between 0 and 1? or 1?

Is there really no use for MD5 anymore?

How do I reattach a shelf to the wall when it ripped out of the wall?

Negative Resistance



Modify bash history from inside subshell in an interactive session


Automatic cleanup of Bash historyBash $PROMPT_COMMAND messing up scroll history displayHow can I programmatically add entries to Bash history and have timestamps work properly?Commands run when terminal is open do not appear in historyBash Executing Script from within Script Causes Echo and Read IssuesCross-session duplicate removal in bash command historyHow can I use vi to edit prompt line of a utility?Bash script order mixup in large redirect of outputshopt -s extdebug in .bashrc not working in script filesWhy is bash history substitution still enabled by default?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I executed



history > before; (history -d $n); history > after


where $n is the number corresponding to the last command I typed in the same interactive session before executing this line,



The result is that the line marked $n is not removed from history. If I remove the parentheses so that the history -d runs in the current shell, it works as documented.



How to understand this behavior? Is it true that all scripts that manipulate history need to be sourced?










share|improve this question






























    1















    I executed



    history > before; (history -d $n); history > after


    where $n is the number corresponding to the last command I typed in the same interactive session before executing this line,



    The result is that the line marked $n is not removed from history. If I remove the parentheses so that the history -d runs in the current shell, it works as documented.



    How to understand this behavior? Is it true that all scripts that manipulate history need to be sourced?










    share|improve this question


























      1












      1








      1








      I executed



      history > before; (history -d $n); history > after


      where $n is the number corresponding to the last command I typed in the same interactive session before executing this line,



      The result is that the line marked $n is not removed from history. If I remove the parentheses so that the history -d runs in the current shell, it works as documented.



      How to understand this behavior? Is it true that all scripts that manipulate history need to be sourced?










      share|improve this question
















      I executed



      history > before; (history -d $n); history > after


      where $n is the number corresponding to the last command I typed in the same interactive session before executing this line,



      The result is that the line marked $n is not removed from history. If I remove the parentheses so that the history -d runs in the current shell, it works as documented.



      How to understand this behavior? Is it true that all scripts that manipulate history need to be sourced?







      bash history






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 21 at 13:15







      Weijun Zhou

















      asked Apr 21 at 11:18









      Weijun ZhouWeijun Zhou

      1,705427




      1,705427




















          1 Answer
          1






          active

          oldest

          votes


















          3














          Each shell process has its own idea of what the command-line history is. When an interactive shell exits it will write its remembered history to ~/.bash_history for the next shell to pick up, but that's the extent of cooperation between shell processes.



          In your command, the () makes the shell fork a copy of itself to run history -d command. The child process starts out with a copy of the parent's internal state, so it knows the history, and is able to make changes to its copy of it.



          However, when the subshell exits, its copy of the history (which was just rewritten) is discarded together with the rest of its internal state). The subshell knows it is a subshell, so it doesn't even bother to write ~/.bash_history.




          A script that is not sourced usually cannot manipulate history at all, because it is interpreted by a fresh non-interactive shell that doesn't even read ~/.bash_history at startup.



          You can get the shell to behave like an interactive shell by specifying it on the command line:



          #!/bin/bash -i
          echo something


          The shell that runs this script will append its commands (which includes both the shebang line and the echo something) to the ~/.bash_history it finds on disk. But of course that doesn't affect the in-memory history copy of the shell process you invoke the script from, and when it exits the changes the script made to ~/.bash_history will be lost anyway.






          share|improve this answer























          • Great answer. It would be better if some reference to manual pages or other documents are included.

            – Weijun Zhou
            Apr 21 at 16:08











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f513653%2fmodify-bash-history-from-inside-subshell-in-an-interactive-session%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Each shell process has its own idea of what the command-line history is. When an interactive shell exits it will write its remembered history to ~/.bash_history for the next shell to pick up, but that's the extent of cooperation between shell processes.



          In your command, the () makes the shell fork a copy of itself to run history -d command. The child process starts out with a copy of the parent's internal state, so it knows the history, and is able to make changes to its copy of it.



          However, when the subshell exits, its copy of the history (which was just rewritten) is discarded together with the rest of its internal state). The subshell knows it is a subshell, so it doesn't even bother to write ~/.bash_history.




          A script that is not sourced usually cannot manipulate history at all, because it is interpreted by a fresh non-interactive shell that doesn't even read ~/.bash_history at startup.



          You can get the shell to behave like an interactive shell by specifying it on the command line:



          #!/bin/bash -i
          echo something


          The shell that runs this script will append its commands (which includes both the shebang line and the echo something) to the ~/.bash_history it finds on disk. But of course that doesn't affect the in-memory history copy of the shell process you invoke the script from, and when it exits the changes the script made to ~/.bash_history will be lost anyway.






          share|improve this answer























          • Great answer. It would be better if some reference to manual pages or other documents are included.

            – Weijun Zhou
            Apr 21 at 16:08















          3














          Each shell process has its own idea of what the command-line history is. When an interactive shell exits it will write its remembered history to ~/.bash_history for the next shell to pick up, but that's the extent of cooperation between shell processes.



          In your command, the () makes the shell fork a copy of itself to run history -d command. The child process starts out with a copy of the parent's internal state, so it knows the history, and is able to make changes to its copy of it.



          However, when the subshell exits, its copy of the history (which was just rewritten) is discarded together with the rest of its internal state). The subshell knows it is a subshell, so it doesn't even bother to write ~/.bash_history.




          A script that is not sourced usually cannot manipulate history at all, because it is interpreted by a fresh non-interactive shell that doesn't even read ~/.bash_history at startup.



          You can get the shell to behave like an interactive shell by specifying it on the command line:



          #!/bin/bash -i
          echo something


          The shell that runs this script will append its commands (which includes both the shebang line and the echo something) to the ~/.bash_history it finds on disk. But of course that doesn't affect the in-memory history copy of the shell process you invoke the script from, and when it exits the changes the script made to ~/.bash_history will be lost anyway.






          share|improve this answer























          • Great answer. It would be better if some reference to manual pages or other documents are included.

            – Weijun Zhou
            Apr 21 at 16:08













          3












          3








          3







          Each shell process has its own idea of what the command-line history is. When an interactive shell exits it will write its remembered history to ~/.bash_history for the next shell to pick up, but that's the extent of cooperation between shell processes.



          In your command, the () makes the shell fork a copy of itself to run history -d command. The child process starts out with a copy of the parent's internal state, so it knows the history, and is able to make changes to its copy of it.



          However, when the subshell exits, its copy of the history (which was just rewritten) is discarded together with the rest of its internal state). The subshell knows it is a subshell, so it doesn't even bother to write ~/.bash_history.




          A script that is not sourced usually cannot manipulate history at all, because it is interpreted by a fresh non-interactive shell that doesn't even read ~/.bash_history at startup.



          You can get the shell to behave like an interactive shell by specifying it on the command line:



          #!/bin/bash -i
          echo something


          The shell that runs this script will append its commands (which includes both the shebang line and the echo something) to the ~/.bash_history it finds on disk. But of course that doesn't affect the in-memory history copy of the shell process you invoke the script from, and when it exits the changes the script made to ~/.bash_history will be lost anyway.






          share|improve this answer













          Each shell process has its own idea of what the command-line history is. When an interactive shell exits it will write its remembered history to ~/.bash_history for the next shell to pick up, but that's the extent of cooperation between shell processes.



          In your command, the () makes the shell fork a copy of itself to run history -d command. The child process starts out with a copy of the parent's internal state, so it knows the history, and is able to make changes to its copy of it.



          However, when the subshell exits, its copy of the history (which was just rewritten) is discarded together with the rest of its internal state). The subshell knows it is a subshell, so it doesn't even bother to write ~/.bash_history.




          A script that is not sourced usually cannot manipulate history at all, because it is interpreted by a fresh non-interactive shell that doesn't even read ~/.bash_history at startup.



          You can get the shell to behave like an interactive shell by specifying it on the command line:



          #!/bin/bash -i
          echo something


          The shell that runs this script will append its commands (which includes both the shebang line and the echo something) to the ~/.bash_history it finds on disk. But of course that doesn't affect the in-memory history copy of the shell process you invoke the script from, and when it exits the changes the script made to ~/.bash_history will be lost anyway.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 21 at 14:47









          Henning MakholmHenning Makholm

          48626




          48626












          • Great answer. It would be better if some reference to manual pages or other documents are included.

            – Weijun Zhou
            Apr 21 at 16:08

















          • Great answer. It would be better if some reference to manual pages or other documents are included.

            – Weijun Zhou
            Apr 21 at 16:08
















          Great answer. It would be better if some reference to manual pages or other documents are included.

          – Weijun Zhou
          Apr 21 at 16:08





          Great answer. It would be better if some reference to manual pages or other documents are included.

          – Weijun Zhou
          Apr 21 at 16:08

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f513653%2fmodify-bash-history-from-inside-subshell-in-an-interactive-session%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Sum ergo cogito? 1 nng

          419 nièngy_Soadمي 19bal1.5o_g

          Queiggey Chernihivv 9NnOo i Zw X QqKk LpB