Find the answer to your Linux question:
Results 1 to 3 of 3
Hi All, I am aware this has probably been covered hundreds of times, so apologies if so. I am fairly new to the linux scripting scene, so again apologies if ...
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Posts
    1

    Exporting in a Shell Script

    Hi All,

    I am aware this has probably been covered hundreds of times, so apologies if so.

    I am fairly new to the linux scripting scene, so again apologies if what I'm saying seems pretty odd and makes no sense.

    I am attempting to write a script for some Linux Fedora test servers I have set up.
    For me to change which domain I have this set to point to, I would have previously changed the HOSTS and profile files manually, however I managed to make a script which changes these easily with the use of one command to launch the script.
    The problem I'm having is getting it to use the "export" command. I am aware this would have to be launched in the parent shell rather than the child and so I made a seperate script which has "export SIP_DOMAIN="test.blah.net" in it and had the first script "source" it. This doesn't work and I've probably done something somewhere that is incredibly stupid.

    Can somebody give me a little push in the right direction? Thanks.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I'm a bit confused.

    So there's two things going on here: sourcing and exporting.

    When you "source" a script, all of the variable declarations in that script are applied to the current shell, instead of a subshell like a normal execution. This is pretty much the only way that a script can affect the current shell.

    Exporting a variable means that the variable definition will be present in commands that are executed by the shell. I will give you an example. I wrote a script that just echos out the value of $a:
    Code:
    alex@niamh:~/test/bash$ a=5
    alex@niamh:~/test/bash$ echo $a
    5
    alex@niamh:~/test/bash$ ./echo_a.sh 
    
    alex@niamh:~/test/bash$ export a
    alex@niamh:~/test/bash$ ./echo_a.sh 
    5
    Does this make sense?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Are you "sourcing" it with the dot command:
    Code:
    . ~/myscript
    That will execute the commands in myscript as if they're in your initial script. Without the dot at the beginning the commands in myscript will ony be in scope while myscript is running and won't apply to the calling script.

Posting Permissions

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