Posts tagged with “typeset -r”

Fighting typeset -r shell environment variables with GDB

When somebody's policy makes varibles readonly, such as HISTFILE, with this in a /etc/profile.d/* file: typeset -r HISTFILE

Lets us modify readonly the environment varibles. This works only if we have gdb installed. Done on RHEL7.9

    export HISTFILE=abd
    -bash: HISTFILE: readonly variable
    $ echo $HISTFILE mailto:/home/myuser@AD-domain/.history/20231107.155621
    $  gdb -ex 'call unbind_variable("HISTFILE")' --pid=$$ --batch
    0x00007f7ec902d46c in waitpid () from /lib64/libc.so.6
    $1 = 0
    [Inferior 1 (process 35393) detached]
    $ HISTFILE=.bash_history
    $ touch .bash_history
    $ ls
    $ cat .bash_history
    HISTFILE=.bash_history
    touch .bash_history
    ls
    $

If gdb is available, then stick this into your .bashrc:

    [ -f /usr/bin/gdb ] && /usr/bin/gdb -ex 'call unbind_variable("HISTFILE")' --pid=$$ --batch' >/dev/null 2>&1

And now we can set up our history with something sensible ( at least sensible for me).

    export HISTFILESIZE=
    export HISTSIZE=
    unset HISTTIMEFORMAT
    export HISTFILE=~/.bash_eternal_history
    PROMPT_COMMAND="history -a; $PROMPT_COMMAND"`

Job done. Back to work.