views:

44

answers:

3

Is there any way to find out the variables available in the scope of the shell script.

the scenario is like this, we are using some third party tools and we can customize the output with creating shell scripts with a particular naming convention. we know that certain parameters are being passed to our custom shell scripts but we want to know what else is being passed.

thanks.

+2  A: 

It's very easy ;)

env
WoLpH
A: 

It sounds like you want environment variables, so use export -p. The output (which consists of lines of the form export variable=value per POSIX) is quoted in such a way that it can be parsed by the shell. It's also sorted by variable name in most shells.

If you want all shell parameters (for your use case, this would be relevant only if the scripts were sourced rather than called as separate programs), use set (again, it's in POSIX, the output is properly quoted for reparsing, and it's sorted in most shells).

Gilles
A: 

The command is set

From the bash manual page

set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
    Without options, the name and value of each shell variable are displayed in a
    format that can be reused  as  input.

Do not confuse this with env which will print out the values of environment variables not shell variables. shell variables can be marked for automatic export into the environment of of subsequent child processes using the export command.

scope as a programming term, only really applies to shell variables - commands like typeset and local can be used in some shells (ksh and bash) to allow the use of scoped shell variables within functions. environment variables are global to a instance of a processes.

Beano