Skip to content

Support for anonymous session (no logging to history) #1156

Closed

Description

@jonhoo

In some session I know I will have to type commands that contain sensitive information, and in this case I would prefer not to have those commands written to history at all.

In bash, this can be done by doing unset HISTFILE before running any commands. You can still use arrow up to get to previous commands from that session, but once you terminate the shell, the commands are gone. I haven't found a similar feature in fish? I know I can prefix each command with a space, but that's not really a good alternative when you have to run more than just a couple of commands.

Activity

  1. IngCr3at1on commented on Aug 21, 2015

    @IngCr3at1on

    I might be missing something somewhere in my search but this seems like a valid enough place to ask. Is there currently a way to clear the history in fish shell?

  2. ridiculousfish commented on Aug 21, 2015

    @ridiculousfish
    Member

    Try history --clear

  3. IngCr3at1on commented on Aug 21, 2015

    @IngCr3at1on

    Awesome, thanks @ridiculousfish

    I had tried history --c but didn't think to try the word clear for some reason lol

  4. zanchey commented on Sep 11, 2015

    @zanchey
    Member

    There's a hacky implementation: add commandline " " to your fish_prompt.

  5. gechr commented on Feb 6, 2016

    @gechr
    Contributor

    I've come up with an even hackier (but effective) solution until this is properly supported in fish.

    In ~/.config/fish/config.fish, add:

    set -g __fish_history_file        ~/.local/share/fish/fish_history
    set -g __fish_history_backup_file $__fish_history_file.bak
    set -g __fish_history_pid_file    $__fish_history_file.pid
    
    if test -f $__fish_history_backup_file
        read -l histpid < $__fish_history_pid_file
        # If the pid no longer has open files, restore the original history file
        if not lsof -p $histpid >/dev/null
            mv -f -- $__fish_history_backup_file $__fish_history_file
            rm -f -- $__fish_history_pid_file
        end
    end

    Then, create a new function to disable history (e.g. ~/.config/fish/functions/disable_history.fish)

    function disable_history --description "Disable history for the current session"
        # Do not continue if there is an existing backup file
        test -f $__fish_history_backup_file; and return
        # Keep track of the pid responsible for disabling history
        echo %self > $__fish_history_pid_file
        # Redirect history to /dev/null
        mv -f -- $__fish_history_file $__fish_history_backup_file
        and ln -sf -- /dev/null $__fish_history_file
    end

    When the function is executed, history will be disabled for the current session and any other sessions that are launched while the current session is active (i.e. the PID exists). Once the current session exits, new sessions will write their history as normal.

    Another function could be written to manually re-enable history, rather than every time a new session is launched.

  6. gechr commented on Feb 6, 2016

    @gechr
    Contributor

    @bucaran It creates a symbolic link from ~/.local/share/fish/fish_history to /dev/null, which is a special device that discards anything written to it. It's important that the original history is first renamed to ~/.local/share/fish/fish_history.bak, though, else you'd lose all your history. Use with caution! 😉

  7. gechr commented on Feb 6, 2016

    @gechr
    Contributor

    Hmm, I'm not quite sure I understand the question, but I'll take another stab at answering it...

    It means none of the history is stored anywhere on disk after you've typed disable_history. If the symlink wasn't created, fish would automatically create a new history file and start writing to it, which is what we're trying to avoid.

  8. krader1961 commented on Mar 1, 2016

    @krader1961
    Contributor

    Closing this as a dup of #102. Once I've addressed that issue it will be possible to tell fish not to log any commands both before invoking the fish shell and while it is running. Not to mention having distinct history log files for specialized needs.

  9. jspiro commented on Mar 7, 2018

    @jspiro

    I'm adding my support for an anonymous shell session. Just because you can do it using other means doesn't diminish the value of first class support by your shell, like web browsers.

    That said, prepending your command with a single space is a subtle but effective way to do this that 99% of people don't know: https://stackoverflow.com/questions/8473121/execute-command-without-keeping-it-in-history

  10. faho commented on Mar 7, 2018

    @faho
    Member

    @jspiro: No need for "adding support". In 3.0 you can set $fish_history to the empty string (this was supposed to work in 2.7.0 but crashed when you did this on startup).

  11. jspiro commented on Mar 8, 2018

    @jspiro
  12. faho commented on Mar 8, 2018

    @faho
    Member

    That’s much more complicated than necessary, like saying you can clear your browser history. That’s all I’m suggesting.

    @jspiro: What exactly is more complicated here? You start an "anonymous session" by setting that variable. If you want something that has a more explicitly "anonymous" name, make your own function. E.g.

    function fishcognito
        env fish_history='' fish
    end

    I don't really see how we can make it any less complicated without making it less general.

  13. locked as resolved and limited conversation to collaborators on Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions