Question

I have an environment variable containing the name of a directory. I am trying to redirect output from an echo command to a text file in a different directory.

For example

DIR="NewDirectory"
mkdir $DIR
echo "Testing" >> "$DIR\file.txt"

Results in a file named NewDirectory\file.txt in the working directory of the script...what exactly am I missing here? The directory is created without issue, so I am not sure what is going on here.

Was it helpful?

Solution

You have to change \ into /:

DIR="NewDirectory"
mkdir -p $DIR
echo "Testing" >> "$DIR/file.txt"

Changed mkdir -p as suggested by @Jord, because -p means: no error if existing, make parent directories as needed

OTHER TIPS

In (or for that matter), the directory separator is a slash (/), not a backslash (\):

DIR="NewDirectory"
mkdir $DIR
echo "Testing" >> "$DIR/file.txt"

Your line

echo "Testing" >> "$DIR\file.txt"

should read

echo "Testing" >> "$DIR/file.txt"

as / is the separator in paths in Linux.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top