Home > Software engineering >  Why a C file name can be saved in a variable just right after writing a bash script?
Why a C file name can be saved in a variable just right after writing a bash script?

Time:10-30

In today's class, we were asked to write a script that runs a C file through the preprocessor and save the result into another file, with the following conditions:

  • The C file name will be saved in the variable $NAMEFILE
  • The output should be saved in the file NAME

One my classmates fastly wrote the following code as the solution:

#!/bin/bash
gcc -E -o NAME $NAMEFILE

I understand all of the code above, except the last part where the variable $NAMEFILE is written, it seems tricky, why can you simply store the C file name in that variable without even declaring it? the GCC manual doesn't seem to explain that, so I just want to get the logics behind this.

CodePudding user response:

Based on the feedback provided in comment section of my post, the $NAMEFILE variable was a environment variable. I was suggested to read the docs related to environment variables, which I will in the following hours.

My problem was solved.

CodePudding user response:

You are conflating gcc operation / features with the behavior of a shell. The gcc manual would not say anything about "storing the filename in a variable" because that's part or the environment where gcc would be run.

in windows, it would look like this:

gcc -E -o NAME %FILENAME%

You're not going to find any explanation of that in the gcc manual.

The shell (bash) is like "the windows explorer". It's what you use to give the computer (OS) instructions. instead of clicking folders you do cd folder and instead of "double click" you type "./foo/command" or just "gcc".

So just keep that in mind and you'll be fine.

  •  Tags:  
  • bash
  • Related