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.

6
  • If it's complicated, you should write the script to a file first. Commented Apr 17, 2016 at 13:55
  • 1
    Variables don't substitute inside single quotes. Would 'cat("'"$ASD"'")' be what you are looking for? Commented Apr 17, 2016 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. Commented Apr 17, 2016 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. Commented Apr 17, 2016 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? Commented Apr 18, 2016 at 10:37

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

0
$ date > filename
$ export filename=len
$ cat 'fi'"$filename"'ame'
Sun Apr 17 16:20:20 CEST 2016

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.