2

I am trying to list the names of the variables that I have declared in my script, so I have been trying with different combinations of:

( set -o posix ; set ) | less

But I am not getting the result that I expected. It gives me an ouput with all variables in addition to my script´s variables. I do not know if I am using this command in the correct way. My intention is to only list the variables´ names that I have declared in my script and get an output like this:


VAR1
VAR2
VAR3
VAR4
…
Trine10
  • 21
  • 1
  • 1
    I'm not sure if bash itself even knows which variables were explicitly defined by you and which variables were exported from the parent shell. You could write a command that prints all unexported variables. But if you export one of your variables inside your script, then that variable won't be listed. – Socowi Jun 17 '20 at 10:51
  • If you look into your script, you will see the variables. If you are just interested in the variables, you can use tabulator completion. Press `$` `Tab` `Tab`. – ceving Jun 17 '20 at 11:23
  • As an aside, you should not use upper case for your private variables; see https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization – tripleee Apr 18 '22 at 17:52

2 Answers2

0

A very simple solution as long as you use the prefix for your own variables. You should do this to avoid accidentally overwriting existing variables. Set defaults returns to a very long list of all defined variables not just yours, so you need to filter it by prefix.

#!/bin/bash
my_var1=aaa
my_var2=`ls`
export my_var3=bbb

compgen -v| awk -F= '/^my_/{print $1}'

# If you use sh shell it remains
#set |awk -F= '/^my_/{print $1}'

result

my_var1
my_var2
my_var3

EDIT: @Socowi rightly suggested using compgen -v for bash instead of set. Compgen is built into bash. Thanks.

Slawomir Dziuba
  • 1,265
  • 1
  • 6
  • 13
  • Parsing the output of `set` can lead to false positives in case of multi-line strings and functions. With bash's `compgen -v` you only get the variable names, which makes things way easier. – Socowi Jun 17 '20 at 11:01
  • @Socowi sh has no compgen function. Can you give an example of a variable declaration that fails? – Slawomir Dziuba Jun 17 '20 at 11:12
  • Yes, `compgen -v` is a bash thing (which shouldn't be a problem looking at the `#! /bin/bash` in your answer). An example for a false positive in bash (encoded as a C-string, since I cannot post literal linebreaks in this comment): `bash -c $'f() { cat < – Socowi Jun 17 '20 at 11:19
  • @Socowi Thanks. However, these are quite special cases. I corrected the answer. – Slawomir Dziuba Jun 17 '20 at 11:39
0

set cannot be told to list only variables declared in a script. What can be done is to temporarily store defined variables at the script's beginning and filter them out at the end, so only variables defined in-between remain:

#!/bin/bash
set -o posix
set|sed s/=.\*// >/tmp/$$

VAR4=4
VAR3=3
VAR2=2
VAR1=1

set|sed s/=.\*//|grep -vf/tmp/$$
rm /tmp/$$

Note Socowi's valuable suggestions for improvement below.

Armali
  • 18,255
  • 14
  • 57
  • 171
  • 1
    A few tips to make this more robust: 1. Add the `-x` option to `grep`. 2. Use `compgen -v` instead of parsing `set`. 3. Store the list of variables inside a variable instead of using a file with a (rather) fixed name. You might also want to point out that this neglects environment variables which where overwritten by the script. – Socowi Jun 17 '20 at 11:06