G'day Alan,
Welcome to the monastery.
The following reproduces my $PS1 (except for the colours):
ken@ganymede: ~/tmp $ echo $PS1 \[\033[0;36m\]\u@\h: \w\n\$ \[\033[0m\] ken@ganymede: ~/tmp $ perl -Mstrict -Mwarnings -E ' my %escape = ( h => do {my $x = `hostname`; chomp $x; $x}, n => "\n", u => $ENV{USER}, w => $ENV{PWD} =~ s/^$ENV{HOME}/~/r, ); my $string = q{\u@\h: \w\n$ }; $string =~ s/\\([hnuw])/$escape{$1}/g; say $string; ' ken@ganymede: ~/tmp $ ken@ganymede: ~/tmp $
Here I've used four different methods to provide values for the %escape keys and a single regex substitution (s/\\([hnuw])/$escape{$1}/g) to perform all the replacements.
I'll leave you to add colour if you want: Term::ANSIColor may be useful for this.
-- Ken
In reply to Re: replace escape sequences in a string
by kcott
in thread replace escape sequences in a string
by alanwevans
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |