There are two workarounds for what you are trying to do, but neither work the way you are trying. The only way* for a program to affect its calling shell is by help from the calling shell.

You can make a make your program emit the prompt string, and then export the value in your shell. Or you can make your program emit the complete export command and then use eval in the shell.

1) emit the prompt string

in .profile or .bashrc, or manually at a prompt:

export PS1=$( make_a_red_prompt.pl )

make_a_red_prompt.pl

#!/usr/bin/perl use strict; use warnings; my $cmd = q( \e[0;31m[\u@\h \W]$ \e[m ); print $cmd , "\n";

2: return the full shell command and use eval.

manually at a prompt, or within a dotfile:

eval $( export_a_red_prompt.pl )
export_a_red_prompt.pl
#!/usr/bin/perl use strict; use warnings; my $cmd = q( export PS1="\e[0;31m[\u@\h \W]$ \e[m " ); print $cmd , "\n";

Here I'm using $( command ) in the shell to return the STDOUT of a command into the current prompt. Equivalent to using backticks: `command`.

Why use a perl subprocess to essentially just echo a string? The advantage would come in from using something like Term::ANSIColor

#!/usr/bin/perl use warnings; use strict; use Term::ANSIColor; my $prompt = colored ("Prompt", 'bold red on_white'); print "export PS1=$prompt\n";

*ok, technically, yes it is possible for a subprocess to affect the caller. But since it involves finding the parent process and directly inserting into its memory space, I really don't think you want to do that.

It does make for a fun answer to that interview question, though.

ps. zsh.org is awesome.


In reply to Re^3: Send Bash Command by spazm
in thread Send Bash Command by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.