0

I am writing a BASH script that reads in java files. I was attempting to cat a java file into a BASH variable by doing the following:

MY_VARIABLE=$( cat myJavaFile.java ) or any number of other BASH commands to set the BASH variable.

The problem comes when I have the following line in the java file:

/***************** or /**

Seems like any line that starts with the first character being '/' what I get in my BASH variable is a ls of the root directory and that line containing the '/' is not in the variable. All other lines are present in the BASH variable from the file. If I add a character in front of a line containing '/' as the first character I get the line.

Can someone explain what I am doing incorrectly or not understanding?

Thanks in advance.

Mathieu
  • 8,840
  • 7
  • 32
  • 45
Rick
  • 1
  • 1
  • 2
    The problem is not with setting the value of the variable. Later in your code, you are expanding the parameter without quoting it, in which case the expansion is subject to glob expansion. For example, use `echo "$MY_VARIABLE"` instead of `echo $MY_VARIABLE`. – chepner Sep 13 '22 at 12:32
  • When a glob doesn't match any files, it's treated literally, which explains why some occurrences of `/*` are not expanded. – chepner Sep 13 '22 at 12:33
  • Run [Shellcheck](https://www.shellcheck.net/) on your code to find problems like this, and many others. – pjh Sep 13 '22 at 12:34
  • Off-topic side note: This is a [UUOC](https://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat) case; the same assignment can be achieved without `fork()`ing an additional process: `my_variable="$(< myJavaFile.java)"`. (Also, avoid uppercase variable names; uppercase names are (by convention) used for environment variables and you don’t want to clash with them at random.) – Andrej Podzimek Sep 13 '22 at 12:42
  • I have also quoted "$MY_VARIABLE" and got the same results the listing of the root directory – Rick Sep 13 '22 at 12:46
  • Also did "$(< myJavaFile.java)"...same results – Rick Sep 13 '22 at 12:47

0 Answers0