views:

156

answers:

4

Hi SO

I have a ridiculous question due to a ridiculous problem.

Normally if I want to get the contents of an environment variable in UNIX shell, I can do

echo ${VAR}

Let's assume, due to my ridiculous situation, that this isn't possible.

How do I get the contents of an environment variable to stdout, without someone who is looking at the command itself (not the output), see the value of the environment variable.

I can picture the solution being something like echo env(NAME_OF_VAR) although I can't seem to find it. The solution has to work in sh.

PS I can't write a script for this, it must be a built in unix command (i know, ridiculous problem)

Thanks (and sorry for the absurdity)

A: 

How about this:

myVariable=$(env  | grep VARIABLE_NAME | grep -oe '[^=]*$');
jens_profile
actually you're probably better off using cut at the end e.g. ... | cut -d '=' -f2-
jens_profile
+2  A: 

Do you mean something like this:

ENV() {
    printf 'echo $%s\n' $1 | sh
}

This works in plain old Bourne shell.

mouviciel
I'd use more quoting myself: `ENV() { printf 'printf "%%s\\n" "${%s}"\n' "$1" | sh; }`
glenn jackman
+5  A: 

printenv VARIABLE_NAME

skwllsp
Ooh, something out of /usr/ucb. Impressive.
pra
A: 

The solution really depends on what the restrictions are why you can't use a simple $VAR. Maybe you could call a shell that doesn't have the restrictions and let this sub-shell evaluate the variable:

bash -c 'echo $VAR'
sth