1

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.

Sachin
  • 1,217
  • 2
  • 11
  • 31
  • 1
    See: [How to cat <> a file containing code?](https://stackoverflow.com/q/22697688/3776858) – Cyrus Jun 26 '20 at 14:49
  • 1
    @Cyrus i tried changing the cat line like this referring to the accepted answer to add the bashslash - cat > loc.txt <<\endmsg , but still it saved the raw output. I don't want to use exact syntax from there because i am quite used to saving file like this , so tried making the minor change to my existing syntax – Sachin Jun 26 '20 at 15:02
  • You should escape no part of the delimiter. No single quotes, double quotes, or backslashes. – Benjamin W. Jun 26 '20 at 15:03
  • You also don't want the single quotes within the here-doc, or you'll get `'location = /usr/bin/chromium-browser'` – Benjamin W. Jun 26 '20 at 15:04
  • 1
    thanks for pointing that typo out – Sachin Jun 26 '20 at 15:10
  • You could use `printf '%s\n' "location - $(which chromium-browser)"`, no here-doc at all. – Benjamin W. Jun 26 '20 at 15:37

1 Answers1

1

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.

seumasmac
  • 2,174
  • 16
  • 7