我有以下bash命令在当前目录中创建一个多行文件:
cat > data.txt <<EOL
Line 1
Another line
Something else
EOL
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个bash别名或函数来运行此命令.我尝试过以下但没有任何反应:
alias create-file="
cat > ~/Desktop/stuffs/data.txt <<EOL
Line 1
Hello world
Another line
EOL"
Run Code Online (Sandbox Code Playgroud)
尝试功能也没有运气:
function npmrcpersonal() {
"cat > ~/Desktop/stuffs/data.txt <<EOL
Line 1
Hello world
Another line
EOL"
}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
你没有引用函数的主体.而且here-doc不应该缩进.
function npmrcpersonal() {
cat > ~/Desktop/stuffs/data.txt <<EOL
Line 1
Hello world
Another line
EOL
}
Run Code Online (Sandbox Code Playgroud)
的EOL,如果它在左边距(除非你使用的标记只会认可<<-EOL,那么它允许缩进,但只有TAB文字,没有空格).here-doc的其余部分不应缩进,因为这些空格将进入文件,您可能不希望这样.