Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
How to determine where an environment variable came from

I wonder if there is a way to determine the location of the file where certain configuration exists. For example know that I have a global parameter TMOUT and I want to change it, but I don't know WHERE it is configured. I'm aware of this question which already was asked here, however I'd like to know if there any other way available except investigating the order of variables appearance according to the env command.

share|improve this question

marked as duplicate by Gilles, Ulrich Dangel, Renan, jasonwryan, Stéphane Gimenez Aug 20 '12 at 17:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

2 Answers 2

up vote 2 down vote accepted

This command will take a long time to run (since it will examine every file in the system) but it should find the file:

find / -type f -print0 | xargs -0 grep -w 'TMOUT=' /dev/null

An alternative approach is to find out under what circumstances it is set, for example by checking whether it is set when the shell is not a login shell (env -i bash) and whether it is (env -i bash --login). You could manually check files in $HOME, for example .profile, .env, .bashrc or .bash_profile. There are also some files in /etc/ which specify environment variables; it could be set in any one of them.

If your login shell is ksh, you will find that $TMOUT is set by the ksh shell itself.


Edit: to explain some of the less obvious features of the command line above:

The idiom find ... -print0 | xargs -0 ... is quite common. The point of using -print0 instead of the default -print is that the file names are terminated with an ASCII NUL in the case of -print0. This means that filenames containing an actual newline will be correctly represented. Passing the -0 option to xargs simply makes xargs understand the same convention.

The practice of routinely passing /dev/null as the first argument to grep is also worth explaining. Since you only get end-of-file when reading from /dev/null, it will never match any non-empty string with grep. Hence there will be no hits for that file. We only specify it on the grep command line to cope correctly with the case where the final time xargs runs grep, there is only one remaining file to be searched. If grep has only one file argument, it lists the hits without specifying the command line. Like so:

~/tmp/t$ echo hello > a
~/tmp/t$ echo hi > b
~/tmp/t$ grep '^h.*' a
hello
~/tmp/t$ grep '^h.*' a b
a:hello
b:hi
share|improve this answer
    
Thank you for your answer! Could you please explain the command line you proposed? Especially the -print0, xargs -0 and /dev/null in the end? Thanks! –  Eugene S Apr 2 '12 at 5:33
add comment

While this is not an exact answer for your question it may answer a portion of it.

StackOverflow: Linux: where are environment variables stored?

EDIT:

When debugging my shell scripts I typically add -x #!/bin/bash -x You can also try running it from the shell as bash -x

# su - test

    test$ bash -x
     unset a r
    + . /etc/bash_completion.d/yast2-completion.sh
       **(file sourced)**
    ++ YAST=/sbin/yast
    ++ YAST_MODLIST=()
       **(variables set from file)**
       #....
    ++ /usr/bin/dircolors -b /etc/DIR_COLORS
    + eval 'LS_COLORS='\''no=00
       #....
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.