I found this which seems close to the solution, but it's not quite there.
EDIT: With JavaScript, I know I can just run test.replace(/\w/g, "*")
, where test
is my username, but I don't know the equivalent for the Terminal.
Ask Different is a question and answer site for power users of Apple hardware and software. It only takes a minute to sign up.
Sign up to join this communityI found this which seems close to the solution, but it's not quite there.
EDIT: With JavaScript, I know I can just run test.replace(/\w/g, "*")
, where test
is my username, but I don't know the equivalent for the Terminal.
The hidden username should be the same length as the original username? Like so:
Alices-MacBook:~ *****
Bobs-MacBook:~ ***
The following code snippet should do the job (and, as a bonus, it'll also hide hostname). Add it to your ~/.bash_profile, or wherever you set PS1. (I created a temporary file - test.sh - for testing, and then sourced that file - '. ./test.sh'. If something had gone spectacularly wrong, and I'd made my prompt illegible, I could simply restart the terminal and be back to my old prompt).
PROMPT_COMMAND=__prompt_command
__hide_string()
{
echo "$1" | sed 's/./\*/g'
}
__prompt_command()
{
PS1="$(__hide_string $HOSTNAME):\W $(__hide_string $USER)\$"
}
This will replace every character in the username with a "*" (it'll also do it for the hostname, to show function reuse). I consider this to be less than ideal - the function gets called (twice - once for user, once for hostname) every time the prompt is displayed (even though the username hasn't changed): with a bit of hacking it should be possible to amend it so that it only calls the '__hide_string' function when PS1 is set (i.e. at login).
Explanation: the __prompt_command function we've defined sets PS1 every time the prompt is displayed. (This is probably overkill, but keeps things 'dynamic'). PS1 should be familiar; the only new stuff is that '\h' and '\u' are replaced with calls to the __hide_string function (and use $HOSTNAME and $USER as arguments). __hide_string is the fun part: it echoes its argument to sed, which replaces every individual character with a '*'.
The PS1
prompt is defined in /etc/bashrc
and the default is usually PS1='\h:\W \u\$ '
. You'd replace the \u
with *****
. You could also just add, e.g export PS1='\h:\W *****\$ '
to your ~/.bash_profile
file, while leaving the system file (/etc/bashrc
) alone.
${USER//?/*}
(i.e. export PS1="\h:\W ${USER//?/*}\$ "). Note that since the username won't change during a session, this only needs to be run once per session (in ~/.bash_profile and possibly also ~/.bashrc) rather than once per prompt (with PROMPT_COMMAND).
Nov 27, 2016 at 18:31
PROMPT_COMMAND
andPROMPTING
in theBASH
manual page? In Terminal typebash
then right-click on it and selectOpen man Page
. Then ⌘F and search for and read about it. Have you googled - how to customize the bash command prompt? This one is good: How to: Change / Setup bash custom prompt (PS1)*****
(those are the special characters I was talking about). I understand that I'd usePRMOPT_COMMAND
to do this before$PS1
starts.