Linux Shell - 将变量内容写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11618696/
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
Shell - Write variable contents to a file
提问by user1546083
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?
我该如何解决这个问题?
采纳答案by pb2q
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
命令用于复制文件(到文件),而不是用于将文本写入文件。
回答by qwertz
If I understood you right, you want to copy $var
in a file (if it's a string).
如果我理解正确,您想复制$var
到一个文件中(如果它是一个字符串)。
echo $var > $destdir
回答by David W.
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 contentsyou 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"
回答by Eric
None of the answers above work if your variable:
如果您的变量,上述答案均无效:
- starts with
-e
- starts with
-n
- starts with
-E
- contains a
\
followed by ann
- should not have an extra newline appended after it
- 以。。开始
-e
- 以。。开始
-n
- 以。。开始
-E
- 包含一个
\
后跟一个n
- 不应在其后附加额外的换行符
and so they cannot be relied upon for arbitrary string contents.
因此不能依赖它们来获取任意字符串内容。
In bash, you can use "here strings" as:
在 bash 中,您可以将“此处字符串”用作:
cat <<< "$var" > "$destdir"
As noted in the comment below, @Trebawa's answer(formulated in the same room as mine!) using printf
is a better approach.
正如下面的评论中所指出的,@Trebawa 的答案(与我在同一个房间中制定!)使用printf
是一种更好的方法。
回答by Trebawa
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"
printf "%s" "$var" > "$destdir"
回答by Christian Hujer
All of the above work, but also have to work around a problem (escapes and special characters) that doesn't need to occur in the first place: Special characters when the variable is expanded by the shell. Just don't do that (variable expansion) in the first place. Use the variable directly, without expansion.
上述所有工作,但还必须解决一个不需要首先出现的问题(转义和特殊字符):当变量被 shell 扩展时的特殊字符。首先不要这样做(变量扩展)。直接使用变量,无需扩展。
Also, if your variable contains a secret and you want to copy that secret into a file, you might want to not have expansion in the command line as tracing/command echo of the shell commands might reveal the secret. Means, all answers which use $var
in the command line may have a potential security risk by exposing the variable contents to tracing and logging of the shell.
此外,如果您的变量包含一个秘密并且您想将该秘密复制到一个文件中,您可能不希望在命令行中进行扩展,因为 shell 命令的跟踪/命令回显可能会泄露该秘密。这意味着,$var
在命令行中使用的所有答案都可能通过将变量内容暴露给 shell 的跟踪和日志记录而具有潜在的安全风险。
Use this:
用这个:
printenv var >file
That means, in case of the OP question:
这意味着,对于 OP 问题:
printenv var >"$destfile"