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

Re: using `myscript.sh` to change current env



Am 07. Sep, 2013 schwätzte Zenaan Harkness so:

moin moin Zenaan,

Rather than all the convolutions of command substitution, how about just
using a function that's in your profile or bashrc?

$ cat /tmp/bashrc function changeps() {
        export PS1=': '
}
$ . /tmp/bashrc $ changeps :

Add arguments to the fx() for your different options :).

ciao,

der.hans

I want to have a script, to change between a few prompts per the arg
supplied. This is so I can quickly change from my glorious
bells-and-whistles prompt to a plain prompt (eg for cut and paste to
debian-user, just "$ " or "# " depending on current user) to a
timestamped prompt (when I have some long-running process, and I want
to see when it finished) etc.

In bash, we cannot run a script plainly, and have that script update
the current shell's env.

We could source the script with 'source' or '.' command, but this
requires a correct location of the script to be entered, which is slow
and not ideal. I want to be able to run the script as a command.

So I thought, run the script in a subshell, executing the result, like:
$ `ps1`

The following 3-line script is meant to test exactly this:

#!/bin/bash
PS1=': '
echo "export PS1=$PS1"

Note that in this example, the desired new prompt is a colon followed
by a single space.
The problem is, when I run this script as `ps1`, I get a changed
prompt, but just to colon, not including the space.

Does anyone know how I might have a space character included in my new
prompt, using this `way` to change my prompt?

TIA
Zenaan




--
#  http://www.LuftHans.com/        http://www.LuftHans.com/Classes/
#  "Science is like sex: sometimes something useful comes out, but
#  that is not the reason we are doing it." -- Richard Feynman

Reply to: