简体   繁体   中英

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

cat test.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

Currently

cat loc.txt

location = '$loc'

Since "which chromium-browser" is actually - /usr/bin/chromium-browser

Expectation

cat loc.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. Its fine its the answer doesn't uses heredoc for achieving this, although preferably it shouldn't be complex for doing something so simple.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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