Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use declare -x in bash

Tags:

bash

shell

Can some one give an example where declare -x would be useful ?

like image 258
Ankur Agarwal Avatar asked Apr 26 '11 03:04

Ankur Agarwal


2 Answers

Use declare -x when you want to pass a variable to a different program, but don't want the variable to be used in global scope of the parent shell (i.e. when declaring inside a function).

From the bash help:

When used in a function, declare makes NAMEs local, as with the local command. The -g option suppresses this behavior.

-x to make NAMEs export

Using + instead of - turns off the given attribute.

So declare -gx NAME=X will effectively behave the same as export NAME=X, but declare -x does not when the declare statements are inside functions.

like image 175
Jon Avatar answered Sep 23 '22 14:09

Jon


declare -x FOO is the same as export FOO. It "exports" the FOO variable as an environment variable, so that programs you run from that shell session would see it.

like image 34
Chris Jester-Young Avatar answered Sep 22 '22 14:09

Chris Jester-Young