Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash中使用printf命令_Bash - Fatal编程技术网

在bash中使用printf命令

在bash中使用printf命令,bash,Bash,我希望输出是hello world\n,但它是hello\n world\n 有人能用一句话解释一下吗。然后,$s变量的展开被拆分为单词,printf看到两个参数并分别使用它们 引用您的变量扩展以防止出现这种情况(您执行变量赋值的方式): #hello world in bash s="hello world" printf "%s\n" $s s="hello world" printf '%s\n' "$s"

我希望输出是
hello world\n
,但它是
hello\n world\n

有人能用一句话解释一下吗。然后,
$s
变量的展开被拆分为单词,
printf
看到两个参数并分别使用它们

引用您的变量扩展以防止出现这种情况(您执行变量赋值的方式):

#hello world in bash

s="hello world"
printf "%s\n" $s
s="hello world"
printf '%s\n' "$s"