Find the answer to your Linux question:
Results 1 to 4 of 4
I'm trying to use an environment variable in a script, but it keeps returning a null value. The thing that is confusing me is that when I use it OUTSIDE ...
  1. #1
    Just Joined!
    Join Date
    Jan 2009
    Posts
    7

    Talking Use environment variables in script?

    I'm trying to use an environment variable in a script, but it keeps returning a null value. The thing that is confusing me is that when I use it OUTSIDE the script, on the command line, it works fine. Why does the $USER variable work ok, but not the $ARCHIVE variable I created? I created it by typing: ARCHIVE="/home/brad/archives/"

    The Script:
    Code:
    #!/bin/bash
    echo $USER
    echo $ARCHIVE
    The Command Line:
    Code:
    $ echo $ARCHIVE
    /home/brad/archives/
    $ ./TEST
    brad
    
    $
    Thanks!

  2. #2
    Just Joined!
    Join Date
    Jan 2009
    Posts
    7
    Ha! I just answered my own question. Wouldn't you know it, as soon as I post the question I figure it out. For anyone else who was wondering, I just changed
    Code:
    $ ARCHIVE="/home/brad/archives/"
    to
    Code:
    $ export ARCHIVE="/home/brad/archives/"
    Apparently this expands the scope so I can use it in my script.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    More specifically, when you export a variable, it is accessible to other programs that are launched from the shell. When you don't export it, it is only available to the shell itself.
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Jan 2009
    Posts
    7
    Thanks for the info!

Posting Permissions

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