0

I have a simple bash script that I want to take a line from a file whilst in a loop and add it as an argument in an iterative process.

The problem is that when I pass in foo it is not being processed and the script still shows $1.

Script:

#/bin/bash
echo 'var FileWriter = Java.type("java.io.FileWriter");
var fw=new FileWriter("./test.txt", true);
fw.write("\n$1");
fw.close();'

Command and output:

writefile.sh foo

➜  Development bash test.sh foo 
var FileWriter = Java.type("java.io.FileWriter");
var fw=new FileWriter("./test.txt", true);
fw.write("\n$1");
fw.close();

I would expect that Bash would pick up the $1 and associate it with the argument passed in. What is missing here?

3therk1ll
  • 2,056
  • 4
  • 35
  • 64

1 Answers1

3

The string you pass into echo is a single-quoted string, so it will not expand $1. You need to use double-quotes (and escape any double-quotes inside):

#/bin/bash
echo "var FileWriter = Java.type(\"java.io.FileWriter\");
var fw=new FileWriter(\"./test.txt\", true);
fw.write(\"\n$1\");
fw.close();"
richyen
  • 8,114
  • 4
  • 13
  • 28