On Tue 03 Sep 2019 at 14:20:11 (-0400), Greg Wooledge wrote: > On Tue, Sep 03, 2019 at 01:07:08PM -0500, David Wright wrote: > > Trap is designed to break the sequential flow of commands. > > I can't see any reason to do that when the OP wants the > > sequence: > > > > execute a script > > print a string > > Are you actually able to discern the OP's desires? I can't. I found > everything they said in this thread completely impenetrable. At first > I thought they wanted to write a script that would change the bash > prompt by setting the PS1 variable....
I thought that at first, too, which is why I posted some of my .bashrc which is a sensible place to set it and is also sourced. When it appeared they just wanted to print the red string in much the same way as printing a string that "rings a bell", ie to attract attention, then I posted the ";" example. The sequence quoted above would work, but typing "print a string" while the script is possibly printing output of its own is error-prone, which is why $ execute a script ; print a string is safer. And it prints regardless, unless you hit ^C in which case it's unnecessary anyway. If the OP wants a different solution, the ball is in their court as far as I'm concerned: I think I quizzed them enough already. > > If you make your screen narrow before you cut and paste, you won't end > > up with this mess, but something instead like: > > > > trap [-lp] [[arg] sigspec ...] > > The command arg is to be read and > > executed when the shell receives > > signal(s) sigspec. If arg is absent > > (and there is a single sigspec) or > > -, each specified signal is reset to > > its original disposition (the value > > it had upon entrance to the shell). > > An improvement to be sure, but you're still omitting the part about EXIT > which was, if I'm not mistaken, the entire purpose of bringing up 'trap' > in this context. man bash is a heavy read at the best of times, and that posting made it almost incomprehensible. I was only quoting the "newspaper column" version to demonstrate its legibility, not its content. I don't know quite what the point of quoting it was in any case. Bash is packaged with man bash so we've all got it. And will it help the OP without an example? > *If* the OP's desire is "Whenever my script exits, whether it's by normal > means or by crashing after an error, I want it to print the terminal > sequence that will turn text red." then the solution would be: > > #!/bin/bash > trap 'tput setaf 1' EXIT > ... rest of the script ... That would have been a lot clearer. > If the OP's desire is something else, then the answer will be something > else, but we can't answer the question until we can figure out what the > question is. Cheers, David.