Home > front end >  how to print all variables in bash script?
how to print all variables in bash script?

Time:08-28

Let's say I have something like this:

#!/bin/bash
var1=1
var2='two'
third='cat'
abcd='dog'
.
.
.
.
something='else'
env

Now I want print all variables declared inside my script.
I tried env, but yea... it prints environment vars not my local ones..
Also cat /proc/$$/environ doesnot give me what I want as its equal to env.
Running my script with more debug info bash -x ./myscript.sh does not suit me.
Is there any trick to list all vars with their values?

CodePudding user response:

You can use this solution: https://stackoverflow.com/a/63459116

#!/bin/bash

set_before=$( set -o posix; set | sed -e '/^_=*/d' )

var1=1
var2='two'
third='cat'
abcd='dog'
something='else'

set_after=$( set -o posix; unset set_before; set | sed -e '/^_=/d' )
diff  <(echo "$set_before") <(echo "$set_after") | sed -e 's/^> //' -e '/^[[:digit:]].*/d'
  •  Tags:  
  • bash
  • Related