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

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

问题描述

在我的bash脚本,还有很多变数,我必须做出什么来拯救他们到文件。
我的问题是如何列出我的脚本声明的所有变量和获取列表是这样的:

  VARIABLE1 = ABC
变量2 =高清
VARIABLE3 = GHI


解决方案

设置将输出变量,遗憾的是它也将输出功能定义也是如此。

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

 (设置-o POSIX;集)|减

管道到,或重定向到您想要的选项。

因此​​,要在短短的剧本获得声明的变量:

 (设置-o POSIX;集)GT; /tmp/variables.before
源脚本
(设定-o POSIX;集)GT; /tmp/variables.after
差异/tmp/variables.before /tmp/variables.after
RM /tmp/variables.before /tmp/variables.after

(或至少基于:-)东西)

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 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 :-) )

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

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