문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top