如何在bash中列出脚本中声明的变量? [英] How to list variables declared in script in bash?

查看:23
本文介绍了如何在bash中列出脚本中声明的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 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

推荐答案

set 会输出变量,不幸的是它也会输出定义的函数.

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

幸运的是 POSIX 模式只输出变量:

Luckily POSIX mode only outputs the variables:

( set -o posix ; set ) | less

管道到 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 :-) )

这篇关于如何在bash中列出脚本中声明的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆