Fwd: Re: [SLE] how to export a variable when in xterm (in bash script in xterm)
---------- Message transmis ---------- Subject: Re: [SLE] how to export a variable when in xterm (in bash script in xterm) Date: Sat, 17 Aug 2002 15:05:21 +0200 From: Alain Barthélemy <bartydeux@gminformatique.com> To: zentara <zentara@zentara.net> Le Samedi 17 Août 2002 13:54, vous avez écrit :
On Sat, 17 Aug 2002 13:26:56 +0200
Alain Barthélemy <bartydeux@gminformatique.com> wrote:
Somebody told of /home/user/.bash_profile but it does not exist in SuSE-7.3. Which file to use to define a global variable in a Bash shell.
In your home directory look for the hidden file .bashrc
Put a line in there like export EDITOR = /usr/bin/joe
and now every NEW xterm will have that in it's environment variables.
You can setup custom bashrc's too. Make a .bashrc1, and .bashrc2, (you can name the files anything you want). Then start an xterm, and it will have the variables from the default .bashrc. Now type "source .bashrc1". Now the xterm has the variables from .bashrc1, but only that xterm, and only until you kill off the xterm.
If you just need to change an environmental variable in the current xterm: export EDITOR=/usr/bin/vim now the $EDITOR ,for that xterm only, is vim, until you kill off the xterm.
Thank you but my problem is not to install another default text editor I'll try to be concrete - I am in a Window Manager (blackbox) - I run a script - in the script there is the lines echo echo "Last scanned file: $LASTFILE" echo echo "Enter new filename: " read fich if not null of course LASTFILE=$fich ..... LASTFILE=$fich export LASTFILE - Thus $LASTFILE "should" be a global variable. - if I leave now the script and type in console: echo "$LASTFILE" I should read: "name_of_fich_I_entered_in_my_script" because $LASTFILE should be a global variable (===> export LASTFILE <====) - but I only read ann old value of $LASTFILE - now - if I type in console mode: - LASTFILE=whatever - export LASTFILE and run my script with the line: echo "$LASTLINE" I read: whatever Thus, the variable is well exported when I type the instruction in console mode (thus in a xterm of course) but not when the same lines are executed via a bash script Now if I leave the X environment (out blackbox thus)and type, on console mode: echo "$LASTLINE" ====> nothing, nothing, nothing Thus a global variable in a xterm console is not the same global variable in console mode with the same user. Thus how to uniformize all these environments and have them talk the same language. -- Alain Barthélemy cassandre@gminformatique.com bartydeux@gminformatique.com http://bartydeux.gminformatique.com -------------------------------------------------------
On Sat, Aug 17, 2002 at 03:06:16PM +0200, Alain Barthélemy wrote:
- now - if I type in console mode: - LASTFILE=whatever - export LASTFILE
and run my script with the line:
echo "$LASTLINE"
I read: whatever
Thus, the variable is well exported when I type the instruction in console mode (thus in a xterm of course) but not when the same lines are executed via a bash script
Now if I leave the X environment (out blackbox thus)and type, on console mode:
echo "$LASTLINE"
====> nothing, nothing, nothing
Thus a global variable in a xterm console is not the same global variable in console mode with the same user.
Thus how to uniformize all these environments and have them talk the same language.
export does not create a "global variable" that persists from one process to another. What it does do is cause sub-processes to get a copy of the original process's variable. Any changes made to that copy by the sub-process do not affect the original copy held by the original process. (And therefore do not get passed on to other sub-processes started by the original process at some later time.) Perhaps you could put the value into a file: #!/bin/bash # script1 myvar="Hello World!" echo $myvar > some_file #!/bin/bash # script2 myvar=$(cat some_file) echo $myvar Test this by running script1 followed by script2: ./script1 ./script2 Hello World! Another possibility is to "source" your scripts. This causes them to as part of the original process so that they share variables: #script3 myvar="Hello Sailor!" #script4 echo $myvar Test this as follows: source script3 source script4 Hello Sailor! As a shortcut, "source" can be replaced by a single dot: . script3 . script4 Hello Sailor! -- Robert C. Paulsen, Jr. robert@paulsenonline.net
participants (2)
-
Alain Barthélemy
-
Robert C. Paulsen Jr.