0

I can find plenty of answers which uses echo, pipes | and sed for that purpose, but I would like to avoid them. So looking for some direct substitution.

I would like to define env var and reuse it inside the character string passed to application command line argument, i.e. Rscript -e '...'.
Below is what I've tried without success.

Rscript -e 'cat("hello\n")'
#hello
export ASD="HELLO!"
Rscript -e 'cat("$ASD\n")'
#$ASD
Rscript -e 'cat("${ASD}\n")'
#${ASD}
Rscript -e 'cat("$(ASD)\n")'
#$(ASD)

I don't want to use echo and | pipes as my actual -e argument to application is much more complex.

jangorecki
  • 16,384
  • 4
  • 79
  • 160
  • If it's complicated, you should write the script to a file first. – chepner Apr 17 '16 at 13:55
  • 1
    Variables don't substitute inside single quotes. Would `'cat("'"$ASD"'")'` be what you are looking for? – rici Apr 17 '16 at 14:34
  • Somebody votes to close with *unclear what you're asking*. Isn't provided example not clear on that? @rici looks fine, will see if it works. – jangorecki Apr 17 '16 at 18:06
  • @rici just tested it in my CI workflow on gitlab and it works like a charm, exactly what I needed, looking forward for your answer to accept it. – jangorecki Apr 17 '16 at 18:52
  • Without knowing the contents of `Rscript`, we can't possibly know how `cat(...)` will be interpreted by it. By using `eval`, you may be able to use the `$ASD` variable within the script after referring to it by name on the command line. But that's probably not the best way to solve your underlying problem. In your example above (or rici's alternative), what happens if `$ASD` contains a quote or a close bracket? – ghoti Apr 18 '16 at 10:37
  • @ghoti `$ASD` is to keep url address. I find rici answer simple and good enough for that. Thanks for your effort. – jangorecki Apr 18 '16 at 14:11

2 Answers2

1

In addition to fun with quotes, you might be able to use positional parameters. Your question fails to provide a Minimal, Complete, Verifiable Example so I can't be sure this is in line with your needs.

bash-4.3$ bash -c 'set -; echo "${0^^} ... ${1/o/0}"' hello world
HELLO ... w0rld
Community
  • 1
  • 1
ghoti
  • 45,319
  • 8
  • 65
  • 104
0
$ date > filename
$ export filename=len
$ cat 'fi'"$filename"'ame'
Sun Apr 17 16:20:20 CEST 2016
Michael Vehrs
  • 3,293
  • 11
  • 10