10

Normally to create a file in a directory I would use:

echo > /home/user/fileName

but, in this case, my directory path I want to create the file in is stored in a variable in my script so I can't write out the path like that. How do I create a new file inside of it?

4 Answers 4

11

If you have a variable myPath, you can use:

echo > "${myPath}/fileName"

as per the following transcript:

pax:~$ export myPath="/tmp"

pax:~$ ls -al /tmp/xyzzy
ls: cannot access /tmp/xyzzy: No such file or directory

pax:~$ echo > "${myPath}/xyzzy"

pax:~$ ls -al /tmp/xyzzy
-rw-r--r-- 1 pax pax 1 2011-12-06 12:30 /tmp/xyzzy
Sign up to request clarification or add additional context in comments.

Comments

3

Just use:

echo > ${WHATEVER}/fileName

Comments

-1

Won't something like

touch $variable/filename 

work?

Comments

-1
 #Enter designated directory

 cd designatedDirectory

 #Create file in directory

 cat > file.txt

 #OR

 touch file.txt

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.