Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list variables declared in script in bash?

Tags:

variables

bash

In my script in bash, there are lot of variables, and I have to make something to save them to file. My question is how to list all variables declared in my script and get list like this:

VARIABLE1=abc VARIABLE2=def VARIABLE3=ghi 
like image 547
lauriys Avatar asked Aug 20 '09 10:08

lauriys


People also ask

How do I see variables in bash?

To check if a variable is set in Bash Scripting, use-v var or-z ${var} as an expression with if command. This checking of whether a variable is already set or not, is helpful when you have multiple script files, and the functionality of a script file depends on the variables set in the previously run scripts, etc.

What is $() in bash script?

$() Command Substitution According to the official GNU Bash Reference manual: “Command substitution allows the output of a command to replace the command itself.

What is $_ in bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.


1 Answers

set will output the variables, unfortunately it will also output the functions defines as well.

Luckily POSIX mode only outputs the variables:

( set -o posix ; set ) | less 

Piping to less, or redirect to where you want the options.

So to get the variables declared in just the script:

( set -o posix ; set ) >/tmp/variables.before source script ( set -o posix ; set ) >/tmp/variables.after diff /tmp/variables.before /tmp/variables.after rm /tmp/variables.before /tmp/variables.after 

(Or at least something based on that :-) )

like image 123
Douglas Leeder Avatar answered Sep 28 '22 07:09

Douglas Leeder