How to get the value of an exported variable in the console

If I wrote

export COMP_WORDS="this words"

I need something like

get-exported COMP_WORDS

expecting something like

COMP_WORDS="this words"

I tried

set | grep COMP_WORDS

but it takes others lines


Solution 1:

To get the value of VARIABLE you can use

echo $VARIABLE

The quotes don't survive though

$ COMP_WORDS="you said what?"
$ echo $COMP_WORDS
you said what?

Unless you quote them...

$ quote='"2b || !2b"'
$ echo $quote
"2b || !2b"

It makes no difference whether you set the variable yourself or not

Solution 2:

You can use printenv:

$ export var=foo
$ printenv var
foo

In alternative to echo you can also use printf.

printf "%s\n" $COMP_WORDS

Solution 3:

In bash, if you want output that's reusable setting the variable again, you can try declare:

$ declare -p USER
declare -x USER="muru"
$ export foo='abc
> def
> hij"
> '"'"
$ declare -p foo
declare -x foo="abc
def
hij\"
'"