PS1: How do I echo colorcodes from functions that get executed in subshells?Get the source directory of a Bash script from within the script itselfHow to check if a program exists from a Bash script?How to change the output color of echo in LinuxIs there a way to write a bash function which aborts the whole execution, no matter how it is called?PS1 correct line wrapping in BashGetting a dynamic bash prompt PS1 rightBash PS1: line wrap issue with non-printing characters from an external commandRewriting commands from history causes pieces of command and PS1 to be deleted and cursor to move unexpectedlyHow can I conditionally include a newline in a bash prompt *without a filler char*How do I color bash PS1 prompt from the result of a function
Can you pop microwave popcorn on a stove?
Golfball Dimples on spaceships (and planes)?
I multiply the source, you (probably) multiply the output!
Template default argument loses its reference type
Are fast interviews red flags?
How do I play this harmonic? (Guitar)
Python reimplementation of Lost In Space by Tim Hartnell
How to best explain that you are taking pictures in a space for practice reasons?
How to apply a register to a command
Project Euler problem #112
How do English-speaking kids loudly request something?
O.5=north.5 (are this the same?)
Compiler optimization of bitwise not operation
If every star in the universe except the Sun were destroyed, would we die?
Laptop failure due to constant fluctuation of AC frequency and voltage
How can I hint that my character isn't real?
After a few interviews, What should I do after told to wait?
How many attacks exactly do I get combining Dual Wielder feat with Two-Weapon Fighting style?
Did the Byzantines ever attempt to move their capital to Rome?
Why has Marx's "Das Kapital" been translated to "Capital" in English and not "The Capital"
Friend is very nit picky about side comments I don't intend to be taken too seriously
How do draw effects during the discard phase work?
What does "先が気になる" mean?
How to make a pipe-divided tuple?
PS1: How do I echo colorcodes from functions that get executed in subshells?
Get the source directory of a Bash script from within the script itselfHow to check if a program exists from a Bash script?How to change the output color of echo in LinuxIs there a way to write a bash function which aborts the whole execution, no matter how it is called?PS1 correct line wrapping in BashGetting a dynamic bash prompt PS1 rightBash PS1: line wrap issue with non-printing characters from an external commandRewriting commands from history causes pieces of command and PS1 to be deleted and cursor to move unexpectedlyHow can I conditionally include a newline in a bash prompt *without a filler char*How do I color bash PS1 prompt from the result of a function
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I've never really touched bash before so it might be that the solution to my problem is obvious, but I can't wrap my head around it. I want to set my PS1 to certain values with certain colors. It works fine until I echo a color code from a subshell, then it gets messed up.
For example:
some_color='33[01;38;5;196m'
PS1="[$some_color] $USER "
Works just fine. Assume I have a function called get_color that echoes the color code with the escape sequence, my prompt prints the sequence [] in front of the username and the bash history is messed up when I hit arrow up a couple times.
some_color='33[01;38;5;196m'
function get_color()
echo -n "[$some_color]"
PS1="$(get_color) $USER "
When I ommit the sequence it prints the right output but the bash history is still messed up.
When I use printf and the sequences 01 and 02 instead of [ and ] it works just fine. Why does it not work with echo?
some_color='33[01;38;5;196m'
function get_color()
printf 01$some_color02"
PS1="$(get_color) $USER "
bash prompt ps1
|
show 1 more comment
I've never really touched bash before so it might be that the solution to my problem is obvious, but I can't wrap my head around it. I want to set my PS1 to certain values with certain colors. It works fine until I echo a color code from a subshell, then it gets messed up.
For example:
some_color='33[01;38;5;196m'
PS1="[$some_color] $USER "
Works just fine. Assume I have a function called get_color that echoes the color code with the escape sequence, my prompt prints the sequence [] in front of the username and the bash history is messed up when I hit arrow up a couple times.
some_color='33[01;38;5;196m'
function get_color()
echo -n "[$some_color]"
PS1="$(get_color) $USER "
When I ommit the sequence it prints the right output but the bash history is still messed up.
When I use printf and the sequences 01 and 02 instead of [ and ] it works just fine. Why does it not work with echo?
some_color='33[01;38;5;196m'
function get_color()
printf 01$some_color02"
PS1="$(get_color) $USER "
bash prompt ps1
Why do you not want to useprintf
Instead ofecho
? It is decidedly the better option.
– tripleee
Mar 28 at 7:16
[
and]
are special syntax unique to PS1, to tell it to start or stop counting characters as printable for purposes of determining curser location / line wrap / etc; they have no meaning in any other context. There's no need to substitute something else in place of them -- you can just leave them out entirely.
– Charles Duffy
Mar 28 at 14:56
...and re: the printf-vs-echo thing, see the POSIX specification forecho
advising thatprintf
be used instead in the APPLICATION USAGE section at pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html
– Charles Duffy
Mar 28 at 14:57
Leaving them out in the function leads to weird behaviour, I assume the shell isn't calculating the prompt length properly. Replacing them with 01 and 02 and printf works though.
– melodyscat
Mar 28 at 18:53
I ended up using printf now but I'm still curious to why those sequences don't work with echo. Are they printf specific?
– melodyscat
Mar 28 at 18:54
|
show 1 more comment
I've never really touched bash before so it might be that the solution to my problem is obvious, but I can't wrap my head around it. I want to set my PS1 to certain values with certain colors. It works fine until I echo a color code from a subshell, then it gets messed up.
For example:
some_color='33[01;38;5;196m'
PS1="[$some_color] $USER "
Works just fine. Assume I have a function called get_color that echoes the color code with the escape sequence, my prompt prints the sequence [] in front of the username and the bash history is messed up when I hit arrow up a couple times.
some_color='33[01;38;5;196m'
function get_color()
echo -n "[$some_color]"
PS1="$(get_color) $USER "
When I ommit the sequence it prints the right output but the bash history is still messed up.
When I use printf and the sequences 01 and 02 instead of [ and ] it works just fine. Why does it not work with echo?
some_color='33[01;38;5;196m'
function get_color()
printf 01$some_color02"
PS1="$(get_color) $USER "
bash prompt ps1
I've never really touched bash before so it might be that the solution to my problem is obvious, but I can't wrap my head around it. I want to set my PS1 to certain values with certain colors. It works fine until I echo a color code from a subshell, then it gets messed up.
For example:
some_color='33[01;38;5;196m'
PS1="[$some_color] $USER "
Works just fine. Assume I have a function called get_color that echoes the color code with the escape sequence, my prompt prints the sequence [] in front of the username and the bash history is messed up when I hit arrow up a couple times.
some_color='33[01;38;5;196m'
function get_color()
echo -n "[$some_color]"
PS1="$(get_color) $USER "
When I ommit the sequence it prints the right output but the bash history is still messed up.
When I use printf and the sequences 01 and 02 instead of [ and ] it works just fine. Why does it not work with echo?
some_color='33[01;38;5;196m'
function get_color()
printf 01$some_color02"
PS1="$(get_color) $USER "
bash prompt ps1
bash prompt ps1
edited Mar 28 at 14:32
melodyscat
asked Mar 28 at 5:56
melodyscatmelodyscat
11 bronze badge
11 bronze badge
Why do you not want to useprintf
Instead ofecho
? It is decidedly the better option.
– tripleee
Mar 28 at 7:16
[
and]
are special syntax unique to PS1, to tell it to start or stop counting characters as printable for purposes of determining curser location / line wrap / etc; they have no meaning in any other context. There's no need to substitute something else in place of them -- you can just leave them out entirely.
– Charles Duffy
Mar 28 at 14:56
...and re: the printf-vs-echo thing, see the POSIX specification forecho
advising thatprintf
be used instead in the APPLICATION USAGE section at pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html
– Charles Duffy
Mar 28 at 14:57
Leaving them out in the function leads to weird behaviour, I assume the shell isn't calculating the prompt length properly. Replacing them with 01 and 02 and printf works though.
– melodyscat
Mar 28 at 18:53
I ended up using printf now but I'm still curious to why those sequences don't work with echo. Are they printf specific?
– melodyscat
Mar 28 at 18:54
|
show 1 more comment
Why do you not want to useprintf
Instead ofecho
? It is decidedly the better option.
– tripleee
Mar 28 at 7:16
[
and]
are special syntax unique to PS1, to tell it to start or stop counting characters as printable for purposes of determining curser location / line wrap / etc; they have no meaning in any other context. There's no need to substitute something else in place of them -- you can just leave them out entirely.
– Charles Duffy
Mar 28 at 14:56
...and re: the printf-vs-echo thing, see the POSIX specification forecho
advising thatprintf
be used instead in the APPLICATION USAGE section at pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html
– Charles Duffy
Mar 28 at 14:57
Leaving them out in the function leads to weird behaviour, I assume the shell isn't calculating the prompt length properly. Replacing them with 01 and 02 and printf works though.
– melodyscat
Mar 28 at 18:53
I ended up using printf now but I'm still curious to why those sequences don't work with echo. Are they printf specific?
– melodyscat
Mar 28 at 18:54
Why do you not want to use
printf
Instead of echo
? It is decidedly the better option.– tripleee
Mar 28 at 7:16
Why do you not want to use
printf
Instead of echo
? It is decidedly the better option.– tripleee
Mar 28 at 7:16
[
and ]
are special syntax unique to PS1, to tell it to start or stop counting characters as printable for purposes of determining curser location / line wrap / etc; they have no meaning in any other context. There's no need to substitute something else in place of them -- you can just leave them out entirely.– Charles Duffy
Mar 28 at 14:56
[
and ]
are special syntax unique to PS1, to tell it to start or stop counting characters as printable for purposes of determining curser location / line wrap / etc; they have no meaning in any other context. There's no need to substitute something else in place of them -- you can just leave them out entirely.– Charles Duffy
Mar 28 at 14:56
...and re: the printf-vs-echo thing, see the POSIX specification for
echo
advising that printf
be used instead in the APPLICATION USAGE section at pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html– Charles Duffy
Mar 28 at 14:57
...and re: the printf-vs-echo thing, see the POSIX specification for
echo
advising that printf
be used instead in the APPLICATION USAGE section at pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html– Charles Duffy
Mar 28 at 14:57
Leaving them out in the function leads to weird behaviour, I assume the shell isn't calculating the prompt length properly. Replacing them with 01 and 02 and printf works though.
– melodyscat
Mar 28 at 18:53
Leaving them out in the function leads to weird behaviour, I assume the shell isn't calculating the prompt length properly. Replacing them with 01 and 02 and printf works though.
– melodyscat
Mar 28 at 18:53
I ended up using printf now but I'm still curious to why those sequences don't work with echo. Are they printf specific?
– melodyscat
Mar 28 at 18:54
I ended up using printf now but I'm still curious to why those sequences don't work with echo. Are they printf specific?
– melodyscat
Mar 28 at 18:54
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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/4.0/"u003ecc by-sa 4.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
);
);
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%2fstackoverflow.com%2fquestions%2f55390999%2fps1-how-do-i-echo-colorcodes-from-functions-that-get-executed-in-subshells%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55390999%2fps1-how-do-i-echo-colorcodes-from-functions-that-get-executed-in-subshells%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
Why do you not want to use
printf
Instead ofecho
? It is decidedly the better option.– tripleee
Mar 28 at 7:16
[
and]
are special syntax unique to PS1, to tell it to start or stop counting characters as printable for purposes of determining curser location / line wrap / etc; they have no meaning in any other context. There's no need to substitute something else in place of them -- you can just leave them out entirely.– Charles Duffy
Mar 28 at 14:56
...and re: the printf-vs-echo thing, see the POSIX specification for
echo
advising thatprintf
be used instead in the APPLICATION USAGE section at pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html– Charles Duffy
Mar 28 at 14:57
Leaving them out in the function leads to weird behaviour, I assume the shell isn't calculating the prompt length properly. Replacing them with 01 and 02 and printf works though.
– melodyscat
Mar 28 at 18:53
I ended up using printf now but I'm still curious to why those sequences don't work with echo. Are they printf specific?
– melodyscat
Mar 28 at 18:54