siutsin
siutsin

Reputation: 1624

Use variable with the EOF to generate a file

I am trying to generate a .js file based on the shell variable following this SO. However, the script still create the file with APP_VERSION: $bumped_version

This is my script:

version=9.9.9

cat > ./constant/app.js <<'msg'
export default Object.freeze({
  APP_VERSION: $bumped_version
})
msg

generated app.js:

export default Object.freeze({
  APP_VERSION: $bumped_version
})

Thanks for helping!

Upvotes: 0

Views: 1434

Answers (1)

grail
grail

Reputation: 930

Heredocs can be used in a few different ways:

  1. Standard - this allows for expansion of parameters, but must have closing word hard against left edge:

cat <<EOF ... EOF

  1. Indented - here you can use tabs to indent both text and closing word

cat <<-EOF [tab][tab]... [tab]EOF

  1. Quoted - this version prevents any expansions from being done, which can be useful if you are using it to create another script with variables. If outputting to a file, all the tabs at the start of each line will be removed

cat <<'EOF' ... EOF

You can also combine 2 and 3.

Upvotes: 4

Related Questions