小编mr_*_*eet的帖子

如何将字符串传递给bash命令作为参数

我有一个包含循环的字符串变量.

loopVariable="for i in 1 2 3 4 5 do echo $i done"
Run Code Online (Sandbox Code Playgroud)

我想将此变量传递给shell脚本中的bash命令.但我总是得到一个错误

bash $loopVariable
Run Code Online (Sandbox Code Playgroud)

我也试过了

bin/bash $loopVariable
Run Code Online (Sandbox Code Playgroud)

但它也行不通.Bash处理字符串给我一个错误.但理论上它执行它.我不知道我做错了什么

bash: for i in 1 2 3 4 5 do echo $i done: No such file or directory
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用while循环这种方法.但得到同样的错误

i=0 
loopValue="while [ $i -lt 5 ]; do make -j15 clean && make -j15 done"
bash -c @loopValue
Run Code Online (Sandbox Code Playgroud)

当我使用bash -c"@loopValue"时,我得到以下错误

bash: -c: line 0: syntax error near unexpected token `done'
Run Code Online (Sandbox Code Playgroud)

当我使用时,只需使用bash -c @loopValue

[: -c: line 1: syntax error: unexpected end …
Run Code Online (Sandbox Code Playgroud)

bash shell

2
推荐指数
1
解决办法
2346
查看次数

标签 统计

bash ×1

shell ×1