Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying all the variables declared in a sh file

I have a test.sh file which contains something like the following

source lib.sh

input=$0

A=aa/bb/cc
B=$A/dd
C=$A/ee

doSomething $B $C

Is it possible to display the values of all the variables so that when I read the code, I don't have to trace their final value row by row.

In my case, I mean to display

input=$0 
A=aa/bb/cc
B=aa/bb/cc/dd
C=aa/bb/cc/ee

I know

bash -x test.sh

can achieve this goal; but I don't want test.sh to be executed. ( Executing the functions in test.sh would delete some important files on my disk.) I only want to know the values of all the variables.

like image 657
Brian Avatar asked Nov 24 '25 18:11

Brian


1 Answers

The concept of 'dry run' in bash actually is not possible. What would be the ouptput e.g in cases like the following:

if some_command; then
  variable=output_1
else
  variable=output_2
fi

In order to determine the script flow you have to execute the some_command which may require running some command and getting its output. This will modify your system (it will depend on the command). Thus, there's no 'safe' way to do what you need without executing the bash script.

In order to test your script will have to simulate the commands which could modify your system (by adding an echo at the beginning e.g). This way you can run the script and see the values for your variables.

like image 182
rkachach Avatar answered Nov 26 '25 12:11

rkachach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!