简体   繁体   English

如何使用heredoc语法保存文件但保留实际变量output?

[英]How to save a file using heredoc syntax but preserve the actual variable output?

cat test.sh猫测试.sh

#! /usr/bin/env bash

#some other commands
#some other commands
loc="$(which chromium-browser)"
cat > loc.txt <<'endmsg'
location = '$loc'
endmsg

I have simplied my script to properly explain my issue.我简化了我的脚本以正确解释我的问题。 I am trying to save the output of a variable in another file using heredoc.But it seems that heredoc simply saves the raw text present inside endmsg tags我正在尝试使用heredoc将变量的output保存在另一个文件中。但似乎heredoc只是保存了endmsg标签内的原始文本

Currently目前

cat loc.txt猫定位.txt

location = '$loc'

Since "which chromium-browser" is actually - /usr/bin/chromium-browser由于“哪个铬浏览器”实际上是 - /usr/bin/chromium-browser

Expectation期待

cat loc.txt猫定位.txt

location = /usr/bin/chromium-browser

is there any way by which i could save the actual variable output in another file and not literally save the raw text.有什么方法可以将实际变量 output 保存在另一个文件中,而不是从字面上保存原始文本。 Its fine its the answer doesn't uses heredoc for achieving this, although preferably it shouldn't be complex for doing something so simple.很好,它的答案不使用heredoc来实现这一点,尽管最好不要因为做这么简单的事情而变得复杂。

Simply remove the single quotes:只需删除单引号:

#! /usr/bin/env bash

#some other commands
#some other commands
loc="$(which chromium-browser)"
cat > loc.txt << endmsg
location = $loc
endmsg

will work fine.会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM