Bash 允许在第二行传递参数
我有没有办法得到一个bash文件并在第二行像这样传递一个参数Bash 允许在第二行传递参数,bash,shell,unix,arguments,Bash,Shell,Unix,Arguments,我有没有办法得到一个bash文件并在第二行像这样传递一个参数 sh -c git status; echo "hello world" 这真难看 sh -c "git status; echo "hello world"" 我正在将sh传递给另一个命令,因此我不能只执行以下操作: git status; echo "hello world" 即使是这样做的方式也很好 echo "hello world" 基本上我需要一种不在新线路上终止的方法 正在寻找将命令传递到xargs的更漂亮的方法
sh -c
git status;
echo "hello world"
这真难看
sh -c "git status; echo "hello world""
我正在将sh传递给另一个命令,因此我不能只执行以下操作:
git status;
echo "hello world"
即使是这样做的方式也很好
echo
"hello world"
基本上我需要一种不在新线路上终止的方法
正在寻找将命令传递到xargs
的更漂亮的方法
sh在终端中
echo \
"hello world"
或
简单地引用一下。带引号的字符串可以扩展到多行
sh -c '
git status
echo "hello world"'
您可以使用引号的缩进和放置来找出最符合您审美期望的内容
# Does this look nicer?
sh -c '
git status
echo "hello world"
'
如果希望进行变量扩展,可能需要更改双引号和单引号
dir=/src/project/
sh -c "
cd $dir
git status
echo 'hello world'
"
请注意,如果希望在-c
参数中使用更复杂的引号(包括单引号和双引号本身),则这会变得很混乱。考虑使用本例中所建议的遗传语法。但是请注意,它通过标准输入将脚本提供给shell,而不是作为命令行参数
对于第二个示例,只需转义换行符
echo \
"hello world"
注意不要在\
和行尾之间放置任何其他空格(或其他字符,包括注释)
包括我在内的一些人发现,如果这些反斜杠都在某个固定的列上对齐,代码更容易阅读
echo \
"hello" \
"happy" \
"fruit gum" \
"world"
您的编辑器可能会对此有所帮助。您的第二个示例将git
视为-c
和状态的参数
、echo
和“hello world”
作为附加位置参数。
echo \
"hello world"
echo \
"hello" \
"happy" \
"fruit gum" \
"world"