-2

I'm a bit stuck with this. I'm delaring a variable at the top of my script, then I am creating a file as part of my script:

app="testing"
cat <<EOF >/etc/init.d/test
#!/bin/bash
args="--emperor $APPCONF/test/$APP.ini"
EOF

It doesn't seem to work though, it seems on the $app variable. Must I do something to this variable to get it to display it's value, "testing" inside the file I create?

Jimmy
  • 12,087
  • 28
  • 102
  • 192
  • What does this have to do with _escaping_ the variable? Escaping is used to prevent something from being evaluated, not to force it to be evaluated. – Barmar Sep 25 '14 at 13:48
  • 1
    It's a newbie question, I'm still learning – Jimmy Sep 25 '14 at 13:49
  • 1
    BTW, BashFAQ #50 is very relevant to the code you're writing here: http://mywiki.wooledge.org/BashFAQ/050 – Charles Duffy Sep 25 '14 at 13:52

4 Answers4

5

Use consistent case. variable names are case sensitive.

dee
  • 526
  • 4
  • 8
  • Thank you dee. Is that the only issue though, I saw mention of doing it like this: ```args="--emperor $APPCONF/test/\$APP.ini"``` – Jimmy Sep 25 '14 at 13:50
  • Only do that if you want the '$' to be literally in the output rather than indicate you want to expand the variable. Try it. – dee Sep 25 '14 at 13:52
2

Let's say you were doing this the Right Way. You'd want to store your data in an array:

args=( --emperor "${appconf}/test/${app}.ini" )

and then convert it to a string for embedding:

printf -v args_str '%q ' "${args[@]}"`

...and use that string inside your heredoc:

#!/bin/bash
args=( $args_str )
EOF

...beyond which, anything inside the script being created would want to expand it as an array:

run_something "${args[@]}"

See BashFAQ #50 for rationale and details.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

Besides using consistent case ($app is different from $APP), you may want to enclose your variable names within brackets - you may get issues if you use spaces in between your variables values otherwise, and it's considered a good practice. For example:

args="--emperor ${APPCONF}/test/${APP}.ini"

That way, $APPCONF does not get confused with ${APP}CONF also. I hope this helps!

jimm-cl
  • 5,124
  • 2
  • 25
  • 27
  • There are some cases where that doesn't help -- what if `APPCONF` has a directory name that contains spaces? – Charles Duffy Sep 25 '14 at 13:54
  • I'm not 100% sure, but shouldn't that be covered by the double quotes? [*3.1. Double-quote parameter (variable) references and command substitutions*](http://www.dwheeler.com/essays/filenames-in-shell.html)... to be honest, never really thought that much about it, so thanks for bringing that up :) – jimm-cl Sep 25 '14 at 14:07
  • the double quotes handle the assignment end, but not the side where you're actually trying to _use_ that argument list somewhere. When you're using it, you need to force it to string-split on the space between `--emperor` and the filename; doing that without also splitting on any spaces inside the expansion of `APPCONF` is where things get tricky. – Charles Duffy Sep 25 '14 at 14:59
  • BTW, `$app` vs `${app}` doesn't make any difference in how spaces are handled. The more verbose syntax is called for if you want to distinguish where an expansion parameter ends -- `${foo}bar` vs `$foobar`, for instance -- or if you want to do some fancy PE operations -- `${foo//bar/baz}`, for instance -- but in the simple case here where each variable name to be expanded is followed by a character that isn't legal within a POSIX variable name, it's unnecessary. – Charles Duffy Sep 25 '14 at 15:02
0

I'm not sure to understand your question. I suppose that you would like to end with a file

/etc/init.d/test

containing the text:

#!/bin/bash
args="--emperor $APPCONF/test/testing.ini"

if so your script should be:

app="testing"
cat <<EOF >/etc/init.d/test
#!/bin/bash
args="--emperor \$APPCONF/test/$app.ini"
EOF
karlacio
  • 176
  • 7