[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Temporary color prompt in bash script



On Sun, Sep 01, 2019 at 03:22:32PM -0400, Lee wrote:

[...]

> You have to source the script instead of running it.

Yes, exactly.

> I'm not entirely clear about a new shell gets it's own env that
> disappears when the shell exits thing, but try this:

It's not only a shell thing. It's a basic Unix process thing:
child processes inherit (well, almost always) their parent's
environment -- more precisely a copy of it. They can change
their copy, but not the parent's. That's by design: lots of
loosely coupled programs calling each other (and that's what
Unix is, after all ;-) would degenerate into an unmaintainable
mess otherwise...

> $ cat setps1
> ##!/bin/bash
> # can i change ps1 from a script?
> echo "FOO = ${FOO}"
> export FOO=BAR
> echo "FOO now = ${FOO}"
> export PS1='\[\e]0;\u@\h:
> \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w
> #\[\033[91m\] '
> 
> $ export FOO=FOO
> $ ./setps1
> FOO = FOO
> FOO now = BAR
> $ echo $FOO
> FOO
> 
> See?  FOO is back to it's pre ./setps1 value
> 
> Now try
> $ source setps1

That's the way. There's a shorthand for "source", which is ".". So instead
of doing

  source setps1

you can say

  . setps1

Cheers
-- tomás

Attachment: signature.asc
Description: Digital signature


Reply to: