Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re^3: Send Bash Command

by spazm (Monk)
on Mar 03, 2010 at 07:49 UTC ( #826351=note: print w/replies, xml ) Need Help??

in reply to Re^2: Send Bash Command
in thread Send Bash Command

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=$( )

#!/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 $( )
#!/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. is awesome.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://826351]
[Discipulus]: yees.. masonry, shaving of the ceiling (dunno if shaving is the right term..) 20hours in the weekend.. so veery good ;=)
[LanX]: yes I worked ...
[LanX]: ... and hope to get a free work day in compensation. :)
choroba went to a museum with the older children. The exhibition was called "American Indians".
[Discipulus]: this and...
[Discipulus]: shaving the ceil

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2018-02-19 10:26 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (261 votes). Check out past polls.