首页 > 解决方案 > 评估变量 - 输出 json 文件内容

问题描述

在 bash shell 中,我正在尝试读取 json 文件并加载到变量

eri@xyz:~/Documents/inbound>e1=$(eval echo $(cat ./deploy/request.json))

在获取该变量的输出后,我看到-bash - command not found了 .json 文件的实际内容

eri@xyz:~/Documents/inbound>"$e1"

-bash: { type:Pipeline, category:Software, risk:4, short_description:sample short description text, description:sample detailed description text, assignment_group: Services - Retail Services, cmdb_ci:Retail Service, u_version:1.0, start_date:2017-01-04 18:00:00, end_date:2017-01-04 19:00:00, backout_plan:see department for standard backout plan, implementation_plan:sample implementation plan, test_plan:sample text plan, production_system:false }: command not found

有没有办法抑制-bash - command not found输出?

标签: linuxbashshell

解决方案


不需要eval- 只是e1=$(< ./deploy/request.json)应该做的伎俩。(感谢@shellter 的语法——你甚至不需要使用cat!)

要显示变量,您需要

echo "$e1"

而不仅仅是"$e1". 与许多编程语言 REPL 不同"$e1",命令行本身不会打印出 的值。$e1相反,它告诉 bash 尝试将 的全部内容解释$e1为命令的名称。它不是命令的名称,因此 bash 会告诉您找不到该名称的命令。


推荐阅读