I would like to copy the contents of a variable (here called var
) into a file.
我想将变量的内容(这里称为var)复制到一个文件中。
The name of the file is stored in another variable destfile
.
文件的名称存储在另一个变量destfile中。
I'm having problems doing this. Here's what I've tried:
我做这件事有困难。这是我试过:
cp $var $destfile
I've also tried the same thing with the dd command... Obviously the shell thought that $var
was referring to a directory and so told me that the directory could not be found.
我也用dd命令尝试过同样的事情……显然,shell认为$var指的是一个目录,因此告诉我无法找到该目录。
How do I get around this?
我该如何解决这个问题呢?
5 个解决方案
#1
67
Use the echo
command:
使用echo命令:
var="text to append";
destdir=/some/directory/path/filename
if [ -f "$destdir" ]
then
echo "$var" > "$destdir"
fi
The if
tests that $destdir
represents a file.
if测试$destdir表示一个文件。
The >
appends the text after truncating the file. If you only want to append the text in $var
to the file existing contents, then use >>
instead:
在截断文件后,>追加文本。如果您只想将$var中的文本附加到文件现有内容中,那么使用>>代替:
echo "$var" >> "$destdir"
The cp
command is used for copying files (to files), not for writing text to a file.
cp命令用于复制文件(对文件),而不是将文本写入文件。
#2
5
If I understood you right you want to copy $var
in a file (if it's a string).
如果我理解对了,您希望在文件中复制$var(如果是字符串)。
echo $var > $destdir
#3
3
echo
has the problem that if var
contains something like -e
, it will be interpreted as a flag. Another option is printf
, but printf "$var" > "$destdir"
will expand any escaped characters in the variable, so if the variable contains backslashes the file contents won't match. However, because printf
only interprets backslashes as escapes in the format string, you can use the %s
format specifier to store the exact variable contents to the destination file:
echo有一个问题,如果var包含-e之类的东西,它将被解释为一个标志。另一个选项是printf,但printf“$var”>“$destdir”将扩展变量中的任何转义字符,因此如果变量包含反斜杠,则文件内容将不匹配。但是,因为printf只能将反斜杠解释为格式字符串中的转义符,所以可以使用%s格式说明符将精确的变量内容存储到目标文件:
printf "%s" "$var" > "$destdir"
打印“%s”“$var”“>”“$destdir”
#4
1
None of the answers above work if your variable:
如果你的变量是:
- starts with
-e
- 开始- e
- starts with
-n
- 开始- n
- starts with
-E
- 开始- e
- contains a
\
followed by ann
- 包含一个\,后面跟着一个n
- should not have an extra newline appended after it
- 在它之后不应该添加额外的换行符
and so they cannot be relied upon for arbitrary string contents.
因此它们不能用于任意字符串内容。
In bash, you can use "here strings" as:
在bash中,可以使用“here string”作为:
cat <<< "$var" > "$destdir"
#5
0
When you say "copy the contents of a variable", does that variable contain a file name, or does it contain a name of a file?
当您说“复制变量的内容”时,该变量是包含文件名,还是包含文件名?
I'm assuming by your question that $var
contains the contents you want to copy into the file:
我假设您的问题是$var包含您想要复制到文件中的内容:
$ echo "$var" > "$destdir"
This will echo the value of $var into a file called $destdir. Note the quotes. Very important to have "$var" enclosed in quotes. Also for "$destdir" if there's a space in the name. To append it:
这将把$var的值返回到名为$destdir的文件中。注意引号。在引号中包含“$var”非常重要。如果名称中有空格,也可以使用“$destdir”。附加:
$ echo "$var" >> "$destdir"
#1
67
Use the echo
command:
使用echo命令:
var="text to append";
destdir=/some/directory/path/filename
if [ -f "$destdir" ]
then
echo "$var" > "$destdir"
fi
The if
tests that $destdir
represents a file.
if测试$destdir表示一个文件。
The >
appends the text after truncating the file. If you only want to append the text in $var
to the file existing contents, then use >>
instead:
在截断文件后,>追加文本。如果您只想将$var中的文本附加到文件现有内容中,那么使用>>代替:
echo "$var" >> "$destdir"
The cp
command is used for copying files (to files), not for writing text to a file.
cp命令用于复制文件(对文件),而不是将文本写入文件。
#2
5
If I understood you right you want to copy $var
in a file (if it's a string).
如果我理解对了,您希望在文件中复制$var(如果是字符串)。
echo $var > $destdir
#3
3
echo
has the problem that if var
contains something like -e
, it will be interpreted as a flag. Another option is printf
, but printf "$var" > "$destdir"
will expand any escaped characters in the variable, so if the variable contains backslashes the file contents won't match. However, because printf
only interprets backslashes as escapes in the format string, you can use the %s
format specifier to store the exact variable contents to the destination file:
echo有一个问题,如果var包含-e之类的东西,它将被解释为一个标志。另一个选项是printf,但printf“$var”>“$destdir”将扩展变量中的任何转义字符,因此如果变量包含反斜杠,则文件内容将不匹配。但是,因为printf只能将反斜杠解释为格式字符串中的转义符,所以可以使用%s格式说明符将精确的变量内容存储到目标文件:
printf "%s" "$var" > "$destdir"
打印“%s”“$var”“>”“$destdir”
#4
1
None of the answers above work if your variable:
如果你的变量是:
- starts with
-e
- 开始- e
- starts with
-n
- 开始- n
- starts with
-E
- 开始- e
- contains a
\
followed by ann
- 包含一个\,后面跟着一个n
- should not have an extra newline appended after it
- 在它之后不应该添加额外的换行符
and so they cannot be relied upon for arbitrary string contents.
因此它们不能用于任意字符串内容。
In bash, you can use "here strings" as:
在bash中,可以使用“here string”作为:
cat <<< "$var" > "$destdir"
#5
0
When you say "copy the contents of a variable", does that variable contain a file name, or does it contain a name of a file?
当您说“复制变量的内容”时,该变量是包含文件名,还是包含文件名?
I'm assuming by your question that $var
contains the contents you want to copy into the file:
我假设您的问题是$var包含您想要复制到文件中的内容:
$ echo "$var" > "$destdir"
This will echo the value of $var into a file called $destdir. Note the quotes. Very important to have "$var" enclosed in quotes. Also for "$destdir" if there's a space in the name. To append it:
这将把$var的值返回到名为$destdir的文件中。注意引号。在引号中包含“$var”非常重要。如果名称中有空格,也可以使用“$destdir”。附加:
$ echo "$var" >> "$destdir"