1

In bash shell I am reading all the files in a folder as follows:

 #!/bin/bash
 FILES=MyFolder/*
 for f in $FILES
 do
    echo "Started $f file..."
    ./$f > $f_july_15.txt
    echo "Finished $f file..."
 done

In line 6 I am trying to save the output of each file in a txt file that contains the name of the input file as follows: $f_july_15.txt. I noticed that the txt file does not get created at all, while the echo calls and the command ./$f get executed correctly. I played a little bit moving $f inside the name, but I can not get the name of the input file in the output file. Can anyone please show me why this is happening and how I can solve this?

1 Answer 1

4

The underscore is a valid character in variable names.

Do:

./"$f" > "${f}_july_15.txt"

Your code was trying to use a variable named f_july_15. You need to use braces to delimit the variable name when it's followed by a character that can be part of a variable name.

Sign up to request clarification or add additional context in comments.

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.