Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, doing exercises in A Practical Guide to Linux Commands, Editors, and Shell Programming by Mark G. Sobell. One of the exercises involves a script that is passed in the ...
  1. #1
    Just Joined!
    Join Date
    Dec 2009
    Posts
    4

    BASH: Accessing the value of the name of an environment variable passed in $1

    Hi, doing exercises in A Practical Guide to Linux Commands, Editors, and Shell Programming by Mark G. Sobell. One of the exercises involves a script that is passed in the name of an environment variable.

    So lets say I have an environment variable $test that I exported with value "testing".

    So lets say the script is named foobar, and is invoked:
    foobar test

    So now the environment variable name test is in $1. But how do I access the actual value of $test, which is "testing", in bash? I tried doing something like:

    value=$("echo \$$1")

    which seems way too hackish. Am I missing something simple here?

    Also, later, the script adds some values to this $test environment variable how do I export the new $test variable value in bash?

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

    Try using
    printenv $1
    in your script.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Just Joined!
    Join Date
    Sep 2008
    Posts
    1

    Thumbs up

    I have tried this on my machine in following way.

    Environment variable is PATH and its value is "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin"

    Script temp.sh
    =======================
    echo "======"
    echo $1
    echo "======"
    temp=$1
    echo "======"
    echo $temp
    =======================

    Invoke as

    sh temp.sh $PATH

    O/P is

    ======
    /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
    ======
    ======
    /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin

  4. #4
    Just Joined!
    Join Date
    Dec 2009
    Posts
    4
    Thanks, that works when the variable is passed in as $PATH. Now what if in the script, I modified the value, and wanted to export those changes to $PATH. How would I do that? In your example the value of $PATH that gets passed in, so how would I go about exporting it back out to PATH?

    In case anyone is wondering, the exercise is to write a bash script that takes in two arguments, name of environment variable, and name of value to append to environment variable if it doesn't already exist in the list. I've gotten the appending part down, I just need to figure out how to export the value once its appended. Its tricky because the environment variable itself is one of the arguments.

Posting Permissions

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