Results 1 to 6 of 6

Thread: How export variable in subshell back out to parent?

  1. #1

    Default How export variable in subshell back out to parent?

    Is there any way to use "export" inside a bash script that will affect the variable in the parent process (i.e. the terminal where the script was run)?

  2. #2
    Join Date
    Mar 2010
    Location
    Austin - Texas
    Posts
    10,140
    Blog Entries
    48

    Smile Re: How export variable in subshell back out to parent?

    Is there any way to use "export" inside a bash script that will affect the variable in the parent process (i.e. the terminal where the script was run)?
    As far as I can tell, you can't send anything back to the parent except a return code, normally 0 if all was well. While not the best solution I know, I often just create a text file in the default folder, containing the return info and just open and read the value in the parent upon a return. Perhaps a better solution will come in for us to see.

    Thank You,
    My Blog: https://forums.opensuse.org/blogs/jdmcdaniel3/

    Software efficiency halves every 18 months, thus compensating for Moore's Law

    Its James again from Austin, Texas

  3. #3
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    4,654

    Default Re: How export variable in subshell back out to parent?

    6tr6tr wrote:

    >
    > Is there any way to use "export" inside a bash script that will affect
    > the variable in the parent process (i.e. the terminal where the script
    > was run)?
    >

    The usual way to do this is to use "source".

    --
    PC: oS 11.3 64 bit | Intel Core2 Quad Q8300@2.50GHz | KDE 4.6.2 | GeForce
    9600 GT | 4GB Ram
    Eee PC 1201n: oS 11.4 64 bit | Intel Atom 330@1.60GHz | KDE 4.6.0 | nVidia
    ION | 3GB Ram

  4. #4
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: How export variable in subshell back out to parent?

    Usual technique is to print out export statement(s) to stdout and have the parent eval the output of the command. But this doesn't work if the command uses stdout for other output. You could do some trickery with printing the export to a different file descriptor and then swapping it with stdout in the parent, if you know how. A bit hairy though.

    Other than this it cannot be done adequately. Using a temp file has the problem that you run into problems if you run more than one invocation of the command simultaneously. For one rarely used command you may never hit the problem but if the command is run in a loop and you run more than one such loop, your odds shorten. Another problem is that if the command is meant to be used by everyone, then you have to use a temp file in a location like /tmp because your current directory might not be writable, and then you have to take precautions that people can only write on their own temp file and cannot remove other people's temp files otherwise they might cause mischief on other people's invocations. So you never see properly written scripts use a bandaid like this.

    You'll have to explain why you want to export upwards. Maybe it isn't necessary for what you want to do.

  5. #5

    Default Re: How export variable in subshell back out to parent?

    I need to run a program that for an annoying reason, requires these really old libusb versions. So I keep them in a folder and call "export LD_LIBRARY_PATH=./" in the terminal before I run the program. It isn't bad but it means a bunch of "cd" and "export" statements and then the command to run the program. It'd be easier to put it all in a bash script so I can type one thing.

    The problem is putting the cd/export statements in the script, means the export isn't valid for my terminal session, but only in the script's subshell.

  6. #6
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: How export variable in subshell back out to parent?

    Put the exports in a file and source the file rather than running it.

    Code:
    . ~/.ld-settings
    old-program
    You could make an alias to reduce the typing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •