Emit zero-width bash prompt sequence from external binary
In bash, how do I encode zero-width sequences into PS1, when those sequences are coming from stdout of an external process or function? How do I implement writes-prompt-sequences-to-stdout
so that it can emit multi-colored text to the prompt?
PS1='$( writes-prompt-sequences-to-stdout )'
I know that, when writing a bash PS1 prompt, I must wrap zero-width sequences in [
]
so bash can compute correct prompt width.
PS1='[e[0;35m]$ [e[00m]'
bash does not print the [
]
and understands the prompt is only 2 characters wide.
How do I move those sequences into an external function? The following does not work, my prompt looks like []$ []
, even though I can run render-prompt
and see it writing the correct sequence of bytes to stdout.
PS1='$( render-prompt )'
function render-prompt {
printf '[e[0;35m]$ [e[00m]'
}
Moving the printf call into PS1 does work:
PS1='$( printf '"'"'[e[0;35m]$ [e[00m]'"'"' )'
I theorized, perhaps bash is scanning the PS1 string before execution to count the number of zero-width bytes. So I tried tricking it by encoding sequences that aren't printed, but it correctly ignores the trick.
PS1='$( printf '"'"'$$$$$'"'"' '"'"'[e[00m]'"'"' )'
My question:
How do I write [
]
sequences to stdout from a function or binary that is invoked via PS1?
bash prompt
New contributor
add a comment |
In bash, how do I encode zero-width sequences into PS1, when those sequences are coming from stdout of an external process or function? How do I implement writes-prompt-sequences-to-stdout
so that it can emit multi-colored text to the prompt?
PS1='$( writes-prompt-sequences-to-stdout )'
I know that, when writing a bash PS1 prompt, I must wrap zero-width sequences in [
]
so bash can compute correct prompt width.
PS1='[e[0;35m]$ [e[00m]'
bash does not print the [
]
and understands the prompt is only 2 characters wide.
How do I move those sequences into an external function? The following does not work, my prompt looks like []$ []
, even though I can run render-prompt
and see it writing the correct sequence of bytes to stdout.
PS1='$( render-prompt )'
function render-prompt {
printf '[e[0;35m]$ [e[00m]'
}
Moving the printf call into PS1 does work:
PS1='$( printf '"'"'[e[0;35m]$ [e[00m]'"'"' )'
I theorized, perhaps bash is scanning the PS1 string before execution to count the number of zero-width bytes. So I tried tricking it by encoding sequences that aren't printed, but it correctly ignores the trick.
PS1='$( printf '"'"'$$$$$'"'"' '"'"'[e[00m]'"'"' )'
My question:
How do I write [
]
sequences to stdout from a function or binary that is invoked via PS1?
bash prompt
New contributor
add a comment |
In bash, how do I encode zero-width sequences into PS1, when those sequences are coming from stdout of an external process or function? How do I implement writes-prompt-sequences-to-stdout
so that it can emit multi-colored text to the prompt?
PS1='$( writes-prompt-sequences-to-stdout )'
I know that, when writing a bash PS1 prompt, I must wrap zero-width sequences in [
]
so bash can compute correct prompt width.
PS1='[e[0;35m]$ [e[00m]'
bash does not print the [
]
and understands the prompt is only 2 characters wide.
How do I move those sequences into an external function? The following does not work, my prompt looks like []$ []
, even though I can run render-prompt
and see it writing the correct sequence of bytes to stdout.
PS1='$( render-prompt )'
function render-prompt {
printf '[e[0;35m]$ [e[00m]'
}
Moving the printf call into PS1 does work:
PS1='$( printf '"'"'[e[0;35m]$ [e[00m]'"'"' )'
I theorized, perhaps bash is scanning the PS1 string before execution to count the number of zero-width bytes. So I tried tricking it by encoding sequences that aren't printed, but it correctly ignores the trick.
PS1='$( printf '"'"'$$$$$'"'"' '"'"'[e[00m]'"'"' )'
My question:
How do I write [
]
sequences to stdout from a function or binary that is invoked via PS1?
bash prompt
New contributor
In bash, how do I encode zero-width sequences into PS1, when those sequences are coming from stdout of an external process or function? How do I implement writes-prompt-sequences-to-stdout
so that it can emit multi-colored text to the prompt?
PS1='$( writes-prompt-sequences-to-stdout )'
I know that, when writing a bash PS1 prompt, I must wrap zero-width sequences in [
]
so bash can compute correct prompt width.
PS1='[e[0;35m]$ [e[00m]'
bash does not print the [
]
and understands the prompt is only 2 characters wide.
How do I move those sequences into an external function? The following does not work, my prompt looks like []$ []
, even though I can run render-prompt
and see it writing the correct sequence of bytes to stdout.
PS1='$( render-prompt )'
function render-prompt {
printf '[e[0;35m]$ [e[00m]'
}
Moving the printf call into PS1 does work:
PS1='$( printf '"'"'[e[0;35m]$ [e[00m]'"'"' )'
I theorized, perhaps bash is scanning the PS1 string before execution to count the number of zero-width bytes. So I tried tricking it by encoding sequences that aren't printed, but it correctly ignores the trick.
PS1='$( printf '"'"'$$$$$'"'"' '"'"'[e[00m]'"'"' )'
My question:
How do I write [
]
sequences to stdout from a function or binary that is invoked via PS1?
bash prompt
bash prompt
New contributor
New contributor
edited 6 hours ago
Jeff Schaller
42.9k1159137
42.9k1159137
New contributor
asked 6 hours ago
cspotcodecspotcode
1462
1462
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I figured it out. Bash special-cases e
, [
, and ]
within PS1. It coverts e
to an escape byte, [
to a 1
byte, and ]
to a 2
byte. External commands must write 1
and 2
bytes to stdout.
According to ASCII, these encode "start of heading" and "start of text."
http://www.columbia.edu/kermit/ascii.html
Here's a working example, which relies on printf converting escapes within the first positional parameter into the correct bytes:
PS1='$( render-prompt )'
function render-prompt {
printf '133[0;35m2$ 133[00m2'
}
render-prompt | hexdump -C
00000000 01 1b 5b 30 3b 33 35 6d 02 24 20 01 1b 5b 30 30 |..[0;35m.$ ..[00|
00000010 6d 02 |m.|
00000012
New contributor
add a comment |
You correctly figured out the 1
, 2
and e
. But there's a better and more correct way to produce the sequences.
The printf
command takes FORMAT as the first argument. This format consumes the subsequent arguments. So your function could be better written this way:
render-prompt () { printf "1%b2%s1%b2" 'e[0;35m' '$ ' 'e[00m'; }
The first argument is "1%b2%s1%b2"
and this is the format. It's a good and logical place for 1
and 2
, which embrace the %b
parameters. (The %b
s are -escaped strings and
%s
represents a standard string.)
The others are 'e[0;35m'
, '$ '
and 'e[00m'
, which correspond to %b
, %s
and %b
in the format. Here I'd put e
s, as they escape the [
braces they precede.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
cspotcode is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504150%2femit-zero-width-bash-prompt-sequence-from-external-binary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I figured it out. Bash special-cases e
, [
, and ]
within PS1. It coverts e
to an escape byte, [
to a 1
byte, and ]
to a 2
byte. External commands must write 1
and 2
bytes to stdout.
According to ASCII, these encode "start of heading" and "start of text."
http://www.columbia.edu/kermit/ascii.html
Here's a working example, which relies on printf converting escapes within the first positional parameter into the correct bytes:
PS1='$( render-prompt )'
function render-prompt {
printf '133[0;35m2$ 133[00m2'
}
render-prompt | hexdump -C
00000000 01 1b 5b 30 3b 33 35 6d 02 24 20 01 1b 5b 30 30 |..[0;35m.$ ..[00|
00000010 6d 02 |m.|
00000012
New contributor
add a comment |
I figured it out. Bash special-cases e
, [
, and ]
within PS1. It coverts e
to an escape byte, [
to a 1
byte, and ]
to a 2
byte. External commands must write 1
and 2
bytes to stdout.
According to ASCII, these encode "start of heading" and "start of text."
http://www.columbia.edu/kermit/ascii.html
Here's a working example, which relies on printf converting escapes within the first positional parameter into the correct bytes:
PS1='$( render-prompt )'
function render-prompt {
printf '133[0;35m2$ 133[00m2'
}
render-prompt | hexdump -C
00000000 01 1b 5b 30 3b 33 35 6d 02 24 20 01 1b 5b 30 30 |..[0;35m.$ ..[00|
00000010 6d 02 |m.|
00000012
New contributor
add a comment |
I figured it out. Bash special-cases e
, [
, and ]
within PS1. It coverts e
to an escape byte, [
to a 1
byte, and ]
to a 2
byte. External commands must write 1
and 2
bytes to stdout.
According to ASCII, these encode "start of heading" and "start of text."
http://www.columbia.edu/kermit/ascii.html
Here's a working example, which relies on printf converting escapes within the first positional parameter into the correct bytes:
PS1='$( render-prompt )'
function render-prompt {
printf '133[0;35m2$ 133[00m2'
}
render-prompt | hexdump -C
00000000 01 1b 5b 30 3b 33 35 6d 02 24 20 01 1b 5b 30 30 |..[0;35m.$ ..[00|
00000010 6d 02 |m.|
00000012
New contributor
I figured it out. Bash special-cases e
, [
, and ]
within PS1. It coverts e
to an escape byte, [
to a 1
byte, and ]
to a 2
byte. External commands must write 1
and 2
bytes to stdout.
According to ASCII, these encode "start of heading" and "start of text."
http://www.columbia.edu/kermit/ascii.html
Here's a working example, which relies on printf converting escapes within the first positional parameter into the correct bytes:
PS1='$( render-prompt )'
function render-prompt {
printf '133[0;35m2$ 133[00m2'
}
render-prompt | hexdump -C
00000000 01 1b 5b 30 3b 33 35 6d 02 24 20 01 1b 5b 30 30 |..[0;35m.$ ..[00|
00000010 6d 02 |m.|
00000012
New contributor
New contributor
answered 6 hours ago
cspotcodecspotcode
1462
1462
New contributor
New contributor
add a comment |
add a comment |
You correctly figured out the 1
, 2
and e
. But there's a better and more correct way to produce the sequences.
The printf
command takes FORMAT as the first argument. This format consumes the subsequent arguments. So your function could be better written this way:
render-prompt () { printf "1%b2%s1%b2" 'e[0;35m' '$ ' 'e[00m'; }
The first argument is "1%b2%s1%b2"
and this is the format. It's a good and logical place for 1
and 2
, which embrace the %b
parameters. (The %b
s are -escaped strings and
%s
represents a standard string.)
The others are 'e[0;35m'
, '$ '
and 'e[00m'
, which correspond to %b
, %s
and %b
in the format. Here I'd put e
s, as they escape the [
braces they precede.
add a comment |
You correctly figured out the 1
, 2
and e
. But there's a better and more correct way to produce the sequences.
The printf
command takes FORMAT as the first argument. This format consumes the subsequent arguments. So your function could be better written this way:
render-prompt () { printf "1%b2%s1%b2" 'e[0;35m' '$ ' 'e[00m'; }
The first argument is "1%b2%s1%b2"
and this is the format. It's a good and logical place for 1
and 2
, which embrace the %b
parameters. (The %b
s are -escaped strings and
%s
represents a standard string.)
The others are 'e[0;35m'
, '$ '
and 'e[00m'
, which correspond to %b
, %s
and %b
in the format. Here I'd put e
s, as they escape the [
braces they precede.
add a comment |
You correctly figured out the 1
, 2
and e
. But there's a better and more correct way to produce the sequences.
The printf
command takes FORMAT as the first argument. This format consumes the subsequent arguments. So your function could be better written this way:
render-prompt () { printf "1%b2%s1%b2" 'e[0;35m' '$ ' 'e[00m'; }
The first argument is "1%b2%s1%b2"
and this is the format. It's a good and logical place for 1
and 2
, which embrace the %b
parameters. (The %b
s are -escaped strings and
%s
represents a standard string.)
The others are 'e[0;35m'
, '$ '
and 'e[00m'
, which correspond to %b
, %s
and %b
in the format. Here I'd put e
s, as they escape the [
braces they precede.
You correctly figured out the 1
, 2
and e
. But there's a better and more correct way to produce the sequences.
The printf
command takes FORMAT as the first argument. This format consumes the subsequent arguments. So your function could be better written this way:
render-prompt () { printf "1%b2%s1%b2" 'e[0;35m' '$ ' 'e[00m'; }
The first argument is "1%b2%s1%b2"
and this is the format. It's a good and logical place for 1
and 2
, which embrace the %b
parameters. (The %b
s are -escaped strings and
%s
represents a standard string.)
The others are 'e[0;35m'
, '$ '
and 'e[00m'
, which correspond to %b
, %s
and %b
in the format. Here I'd put e
s, as they escape the [
braces they precede.
answered 4 hours ago
TomaszTomasz
9,96452966
9,96452966
add a comment |
add a comment |
cspotcode is a new contributor. Be nice, and check out our Code of Conduct.
cspotcode is a new contributor. Be nice, and check out our Code of Conduct.
cspotcode is a new contributor. Be nice, and check out our Code of Conduct.
cspotcode is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504150%2femit-zero-width-bash-prompt-sequence-from-external-binary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown