从Bash运行Perl/Python/Ruby行

asp*_*pin 0 ruby python bash shell perl

我想在shell脚本中运行Ruby/Python一行代码,例如:

python 'print "hello world"'
Run Code Online (Sandbox Code Playgroud)

要么

ruby 'puts "hello world"'
Run Code Online (Sandbox Code Playgroud)

或类似的东西,所以我可以快速输入其他地方的输入.

Joh*_*ooy 6

从版本2.05b开始,bash可以使用<<<运算符从"here string"重定向标准输入(stdin).

$ python <<< 'print "hello world"'
hello world
$ ruby <<< 'puts "hello world"'
hello world
Run Code Online (Sandbox Code Playgroud)


Tom*_*ech 5

这些命令中的每一个都有一个开关来执行作为参数传递的字符串.对于python,它是-c:

$ python -c 'print "hello world"'
hello world
Run Code Online (Sandbox Code Playgroud)

对于红宝石,它是-e:

$ ruby -e 'puts "hello world"'
hello world
Run Code Online (Sandbox Code Playgroud)

顺便说一句,输出python --help,ruby --help或看在man页面或者程序会给你答案.