简体   繁体   English

如何从npm运行的bash脚本中在$ 1之前打印$ 2?

[英]How do I print $2 BEFORE $1 from a bash script run by npm?

so i'm confused about the relationship between my custom npm scripts and the bash scripts they run. 所以我对自定义npm脚本和它们运行的​​bash脚本之间的关系感到困惑。 EDIT : i don't think this an isolated bash issue. 编辑 :我不认为这是一个孤立的bash问题。 i can produce the expected behavior described below by writing echo $2 $1 into an .sh file and running it directly from the terminal 我可以通过将echo $2 $1写入.sh文件并直接从终端运行它来产生以下所述的预期行为

for example 例如

"scripts": {"report":"echo $2 $1"}

if i run it from the terminal: npm run report "first" "second" because in my npm script, i call $2BEFORE$1 , i expect it to output this: second first , but for some reason it always prints $1 first: first second (edited) 如果我从终端运行: npm run report "first" "second" ,因为在我的NPM剧本,我叫$2 之前 $1 ,我期待它输出这样的: second first ,但由于某些原因,它始终打印$1第一: first second (编辑)

i tried a workaround by caching my arguments as variables and then print those: 我通过将参数作为变量缓存,然后打印了这些内容,从而尝试了一种解决方法:

"scripts": {"report": "(FIRST=$1 && SECOND=$2) && echo $SECOND $FIRST"}

but same output: npm run report "first" "second" => first second 但输出相同: npm run report "first" "second" => first second

what gives? 是什么赋予了?

The following bash file works good for me: 以下bash文件对我有用:

echo $2 $1

Run with: 运行:

bash file.sh 1 2

Output: 输出:

2 1

EDIT*: 编辑*:

The problem is with the way you try to use node. 问题在于您尝试使用节点的方式。 It seems the $X variables are not populated by node. 看来$ X变量未由节点填充。 It just appends your arguments to the rest of your command. 它只是将您的参数附加到命令的其余部分。

EDIT*2: 编辑* 2:

workaround 解决方法

{
"scripts": {"report":"a(){ echo $2 $1; };a"}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM