How to prevent saving bash commands *with* space in front

I read that, in order to not save bash commands to .bash_history, you put a space in front. This worked on all my embedded Linux PCs but not on my desktop with Ubuntu 18.04 LTS. I just thought it was an Ubuntu 'quirk' or something. Now I've installed a second PC with Ubuntu 18.04 LTS and it works as expected: commands with a space in front are not saved!

So there's possibly something wrong with the first PC with Ubuntu 18.04 LTS. How can I figure out what's causing this and fix it so it doesn't save bash commands with space in front?

Excerpt:

mark@mark:~$ tail -n 5 .bash_history 
./deploy.sh 
./deploy.sh 
upd
 man ls
nano .bash_history 
mark@mark:~$ 

Possibly useful stuff:

mark@mark:~$ cat .bashrc 
alias upd="sudo apt update && sudo apt upgrade && sudo apt autoremove"
export PATH="$PATH:"/opt/microchip/xc8/v1.44/bin""

mark@mark:~$ printenv
CLUTTER_IM_MODULE=xim
LC_MEASUREMENT=en_US.UTF-8
LC_PAPER=en_US.UTF-8
LC_MONETARY=en_US.UTF-8
XDG_MENU_PREFIX=gnome-
KIGITHUB=https://github.com/KiCad
LANG=en_US.UTF-8
MANAGERPID=1252
DISPLAY=:0
INVOCATION_ID=26457eadc1b8429692b7f3bd70c6464f
UNITY_DEFAULT_PROFILE=unity
COMPIZ_CONFIG_PROFILE=ubuntu
GTK2_MODULES=overlay-scrollbar:overlay-scrollbar
GTK_CSD=0
COLORTERM=truecolor
ZEITGEIST_DATA_PATH=/home/mark/.local/share/zeitgeist
USERNAME=mark
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
MANDATORY_PATH=/usr/share/gconf/unity.mandatory.path
LC_NAME=en_US.UTF-8
USER=mark
DESKTOP_SESSION=unity
QT4_IM_MODULE=xim
TEXTDOMAINDIR=/usr/share/locale/
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/24f3cfef_6815_4cfb_8cdc_6c63814db4b6
DEFAULTS_PATH=/usr/share/gconf/unity.default.path
PWD=/home/mark
HOME=/home/mark
JOURNAL_STREAM=9:1719321
TEXTDOMAIN=im-config
QT_ACCESSIBILITY=1
XDG_SESSION_TYPE=x11
COMPIZ_BIN_PATH=/usr/bin/
XDG_DATA_DIRS=/usr/share/unity:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
XDG_SESSION_DESKTOP=unity
LC_ADDRESS=en_US.UTF-8
SSH_AGENT_LAUNCHER=gnome-keyring
LC_NUMERIC=en_US.UTF-8
GTK_MODULES=gail:atk-bridge:unity-gtk-module
PAPERSIZE=letter
WINDOWPATH=3
GNOME_SESSION_XDG_SESSION_PATH=
TERM=xterm-256color
VTE_VERSION=5202
SHELL=/bin/bash
QT_IM_MODULE=ibus
XMODIFIERS=@im=ibus
IM_CONFIG_PHASE=2
XDG_CURRENT_DESKTOP=Unity:Unity7:ubuntu
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
GNOME_TERMINAL_SERVICE=:1.617
UNITY_HAS_3D_SUPPORT=true
SHLVL=3
LC_TELEPHONE=en_US.UTF-8
GDMSESSION=unity
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
LOGNAME=mark
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
XDG_RUNTIME_DIR=/run/user/1000
XAUTHORITY=/run/user/1000/gdm/Xauthority
XDG_CONFIG_DIRS=/etc/xdg/xdg-unity:/etc/xdg
PATH=/home/mark/bin:/home/mark/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/opt/microchip/xc8/v1.44/bin:/opt/microchip/xc8/v1.44/bin:/opt/microchip/xc8/v1.44/bin
LC_IDENTIFICATION=en_US.UTF-8
LD_PRELOAD=libgtk3-nocsd.so.0:libgtk3-nocsd.so.0
SESSION_MANAGER=local/mark:@/tmp/.ICE-unix/19058,unix/mark:/tmp/.ICE-unix/19058
GTK_IM_MODULE=ibus
LC_TIME=en_US.UTF-8

Solution 1:

I guess somehow at your first Ubuntu machine, the HISTCONTROL's value has been compromised with.

Open ~/.bashrc in an editor, add a this at the end of file:

HISTCONTROL=ignoreboth

Now if yo open a new terminal, it should ignore saving commands to history when they start with an space.

man bash

HISTCONTROL

A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups. A value of erasedups causes all previous lines matching the current line to be removed from the history list before that line is saved. Any value not in the above list is ignored. If HISTCONTROL is unset, or does not include a valid value, all lines read by the shell parser are saved on the history list, subject to the value of HISTIGNORE. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTCONTROL.