0

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.

5
  • What special characters? Have you read PROMPT_COMMAND and PROMPTING in the BASH manual page? In Terminal type bash then right-click on it and select Open 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) Nov 27 '16 at 4:50
  • @user3439894 I've looked at those but I still don't understand how to change my username to ***** (those are the special characters I was talking about). I understand that I'd use PRMOPT_COMMAND to do this before $PS1 starts. Nov 27 '16 at 4:58
  • You really should clarify the question to explain that you want to replace the username in the command prompt, not in other contexts that it appears in Terminal. Nov 27 '16 at 18:33
  • @GordonDavisson I did and my question got answered. Nov 27 '16 at 18:34
  • @NetOperatorWibby You got the answer you wanted, but part of the point of this site is to let other people with the same (or similar) question find yours & its answers. Therefore, it's best to make your questions as clear, unambiguous, and search-term-friendly as possible. Nov 27 '16 at 18:40
1

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 '*'.

1
  • Thank you! I use zsh-shell so I had to customize a bit for my theme but the end result is still the same. :D Nov 27 '16 at 18:24
1

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.

2
  • Hmm, so there's no way to do this dynamically? I'd want the amount of asterisks to match the amount of characters in my username, without setting the length manually. Nov 27 '16 at 6:00
  • 1
    You could get a string of asterisks the same length as the username with ${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 '16 at 18:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .