使用 Bash 在特定目录中创建文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8395358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:08:21  来源:igfitidea点击:

Creating a file in a specific directory using Bash?

filebashdirectory

提问by Janikole

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?

但是,在这种情况下,我想在其中创建文件的目录路径存储在脚本中的一个变量中,因此我无法像这样写出路径。如何在其中创建一个新文件?

回答by paxdiablo

If you have a variable myPath, you can use:

如果你有一个变量myPath,你可以使用:

echo >${myPath}/fileName

as per the following transcript:

根据以下成绩单:

pax:~$ export myPath=/tmp

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

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

pax:~$ ls -al /tmp/x*
-rw-r--r-- 1 pax pax 1 2011-12-06 12:30 /tmp/xyzzy

回答by David Schwartz

Just use:

只需使用:

echo > ${WHATEVER}/fileName

回答by jmr333

 #Enter designated directory

 cd designatedDirectory

 #Create file in directory

 cat > file.txt

 #OR

 touch file.txt

回答by Jaco Van Niekerk

Won't something like

不会像

touch $variable/filename 

work?

工作?