1

On the terminal how can I easily distinguish between

  1. a shell variable: foo=foovalue

and

  1. an exported environment variable: export bar=barvalue, usable by child processes ?

I know of set and env commands. set reports both cases, without indicating which is which. env does not report 1).

Sample terminal session

(on zsh + macos but that shouldn't matter overmuch):

$foo=foovalue           
$export bar=barvalue    

$set | egrep "^foo|^bar"
bar=barvalue
foo=foovalue

$env | egrep "^foo|^bar"
bar=barvalue

Any way to see both foo and bar, but also indicate that foo is a bash-only variable that will NOT be visible to child processes like a Python or Ruby script? Typically, when my scripts don't see what I expect, what I end up doing is grepping my shell scripts and looking for where the variable was defined. It works, but it's clunky at best.

JL Peyret
  • 10,917
  • 2
  • 54
  • 73
  • 2
    `declare -p foo bar`, and see if the output says `declare --` or `declare -x` – Charles Duffy Aug 03 '23 at 18:57
  • If you want the variable export it, then `export foo`, and you know that it's exported. If you don't wan tit exported, `export -n foo` and then you know. – Kaz Aug 03 '23 at 18:57
  • ...there's also a bash 5.0 feature that'll let you retrieve the flags; which version of bash are you targeting? – Charles Duffy Aug 03 '23 at 18:57
  • (note that the feature I'm considering **is** bash-specific, and will not work on zsh; zsh is not bash, nor is it compatible with bash, and questions about zsh should not say "bash" anywhere in them). – Charles Duffy Aug 03 '23 at 18:58
  • @CharlesDuffy `declare -p` would be a great answer. And, yes, it also worked in zsh - I just checked. I know they are not the same, but they are "close enough most of the time", at least from a user, not sysadmin, perpspective and looking for zsh-only answers often comes up empty. This isn't a sysadmin script, it's troubleshooting my environment variables in an interactive session. – JL Peyret Aug 03 '23 at 19:00
  • The bash 5.0 feature I was considering whether to propose is `[[ ${foo@a} = *x* ]]` -- _does_ that work in zsh? – Charles Duffy Aug 03 '23 at 19:02
  • Interactive troubleshooting is off-topic here and belongs on [unix.se]. – Charles Duffy Aug 03 '23 at 19:03
  • No, `[[ ${foo@a} = *x* ]]` choked in zsh. `declare -p | egrep 'foo|bar'` gave me `export bar=barvalue typeset foo=foovalue` on 2 lines. Which is just what I needed! bash and zsh commands are on-topic here though and this just ends up reminding us that `declare -p` exists and complement `set` and `env`. – JL Peyret Aug 03 '23 at 19:04
  • Tagging and titleing your zsh questions "bash" is a good way to ensure that there _continue_ to be few on-point search results for questions about zsh in the future; it perpetuates the problem. – Charles Duffy Aug 03 '23 at 19:07
  • @CharlesDuffy yes, I have now both tags. Both bash and zsh folk can use it. – JL Peyret Aug 03 '23 at 19:09
  • (...whereas tagging zsh questions "bash" annoys the bash community much the same way as tagging C++ questions C annoys the C community) – Charles Duffy Aug 03 '23 at 19:09
  • Yet another possibility: `printenv foo`. Since _printenv_ is an external command, doing it this way is even shell-agnostic. – user1934428 Aug 04 '23 at 06:55
  • 1
    @JLPeyret : Having both tags, bash and zsh, is a bad idea. What are you going to do if you get one splendid answer for bash, and another splendid one for zsh? Which of those will you accept? – user1934428 Aug 04 '23 at 06:58

2 Answers2

3

Use declare -p and inspect the output. For example:

declare -- BASH="/bin/bash"
declare -x EDITOR="vim"

It's obvious which one is exported or not.

iBug
  • 35,554
  • 7
  • 89
  • 134
  • looks a bit different on zsh, as noted. but that's A-OK, by me, I am not parsing the results, just evaluating them with my Mk I eyeballs. – JL Peyret Aug 03 '23 at 19:09
0
variable_name="some value"
export variable_name

Check if the variable was exported:

declare -p variable_name | grep -q 'declare -x'
if [ $? -eq 0 ]; then
  echo "The variable was exported."
else
  echo "The variable was not exported."
fi
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    [Why is checking `$?` to see if a command succeeded or not an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) -- better to run `if declare -p variable_name | grep -q 'declare -x'; then` and avoid the extra `[` step. – Charles Duffy Aug 03 '23 at 19:00
  • 1
    ...that said, the OP appears to actually be running zsh, not bash, for which `declare -p` can output `typeset` commands and won't match the `declare -x` pattern at all. – Charles Duffy Aug 03 '23 at 19:01
  • Also, consider `unexported_var='declare -x'` as a case the above code fails with. – Charles Duffy Aug 03 '23 at 19:01