Percent character breaks Terminal

bashterminal

If I type the % character in the Terminal, it breaks that shell and every new shell I open until reboot:

$ seq 10 | parallel -j50% "echo Hi {}"
-bash: printf: `"': invalid format character

Does anybody know why this is?

To expand a bit more, if I open a Terminal, and echo "100%", this happens:

Last login: Fri Nov 21 08:28:47 on ttys033
~$ echo "100%"
-bash: printf: `"': invalid format character

0; echo "100~$ 
-bash: printf: `"': invalid format character
7;file://mac-122667.dhcp.fnal.gov/Users/perdue~$ 
-bash: printf: `"': invalid format character
7;file://mac-122667.dhcp.fnal.gov/Users/perdue~$ 
-bash: printf: `"': invalid format character

Every time I press RET after putting a percent character in, the Terminal flashes and prints an error statement about printf. I was actually working on this more after I posted the question and one thing I did was upgrade bash:

~$ echo $BASH_VERSION 
4.3.30(1)-release

This helps. Previously it was Bash 2.something (if I recall – whatever Mountain Lion's default is). In that version of Bash, every open Terminal window aside from the one where I put the % was okay, but every new one I opened was also corrupted and nothing could get the Terminal back but rebooting.

Now, with Bash 4.3, I can type a command to get the prompt back to normal. And new Terminal windows are also okay.

But – I still can't use % in commands I put into the Terminal. Fortunately, I don't need to often, but things like awk just don't work for me on my Mac.

So, my $PS1 is:

~$ echo $PS1
\[\e[0;34m\]\W\[\e[0;31m\]$ \[\e[0m\]

If that looks confusing at first, these are colors I am setting in my bash_profile:

# colours for use in prompts
RED="\[\e[0;31m\]"
GREEN="\[\e[0;32m\]"
YELLOW="\[\e[0;33m\]"
BLUE="\[\e[0;34m\]"
PURPLE="\[\e[0;35m\]"
CYAN="\[\e[0;36m\]"
export PS1="${BLUE}\W${RED}\$ ${NOCOLOR}"

If I echo "100%" with another user account, it is okay! So clearly, I am shooting myself in the foot somewhere in my .bash_profile. Which is too bad, because I basically like my setup, or so I think, ha!

If anybody wants to look at my dot files they're now on GitHub. (I am a bit embarrassed to show them – there is some cruft there, haha.)

Best Answer

Looking at your .bash_profile, this seems to be the most suspicious line:

trap 'printf "${OPENTITLEBAR} `history 1 | cut -b8-` - `pwd` ${CLOSETITLEBAR}"' DEBUG

It is doing a printf on the history (which will contain your % character).

I have no idea what that is intended to do (update window title with last command?), but I would guess if you comment it out, the error will stop.