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
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.
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