[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: setting a variable from a function called from PS1
From: |
Greg Wooledge |
Subject: |
Re: setting a variable from a function called from PS1 |
Date: |
Wed, 25 Aug 2010 14:31:02 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Wed, Aug 25, 2010 at 11:20:28AM -0500, E R wrote:
> I've been trying to get a function called from PS1 to set a variable, e.g.:
>
> num=1
>
> function xyz {
> ((num++))
> date; echo "num: $num"
> }
>
> PS1="\$(xyz): "
The problem here is that the command substitution (the $(...) bit)
creates a subshell. The function is executed in a subshell, so any
changes it makes to global variables are lost when the subshell exits.
> In this example, the global value of num doesn't change if it is
> called from PS1. However num does get incremented if xyz is called
> directly.
>
> Is there a way I can have a function called from PS1 change the value
> of a global variable?
The easiest way would be not to use a function at all:
imadev:~$ PS1='$((++number))\$ '
1$
2$
3$