I want to quote the current directory in my prompt.
Eg, if I do:
mkdir $'new\nline'; cd $'new\nline'
I want my prompt to display $'new\nline'
, and NOT print a literal newline.
I'm seeing interesting behaviour trying to print backslashes (\
) in bash 5.0.9
:
p='\\n' && echo -E "${p@P}" # 2 slashes; output = '\n'
p='\\\\n' && echo -E "${p@P}" # 4 slashes; output = '\n'
p='\\\\\\n' && echo -E "${p@P}" # 6 slashes; output = '\\n'
p='\\\\\\\\n' && echo -E "${p@P}" # 8 slashes; output = '\\n'
Why is the output equivalent with (2 and 4), and (6 and 8) slashes?
Given this confusion, and:
mkdir '\\n' && cd '\\n'
I couldn't work out how to programatically transform \\n
into a string such that it was displayed in a prompt either as: \\\\n
or $'\\\\n'
, as well as handling the literal newline case.
How do I get directory names quoted in the prompt such that:
~
is displayed for$HOME
and a leading~/
for subdirectories- Other paths are escaped only if required
- A copy-paste of the displayed string is a valid shell token referring to the current directory
Eg "$HOME/dir with spaces"
should be displayed as either:
~/dir\ with\ spaces
~/$'dir with spaces'
~/'dir with spaces'