Bash 脚本将 cat 输出存储在变量中,然后回显它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40192725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:26:46  来源:igfitidea点击:

Bash Script store cat output in variable and then echo it

bashunix

提问by Sumeet Masih

I am trying to store a cat output into a variable and then trying to echo it. and then I would like to kill the process.

我正在尝试将 cat 输出存储到一个变量中,然后尝试对其进行回显。然后我想终止这个进程。

#!/bin/bash

var = $(cat tmp/pids/unicorn.pid)

echo $var
sudo kill -QUIT $var

Please if anyone can tell where I am going wrong

请如果有人能告诉我哪里出错了

回答by user000001

Variable assignments in bash should not have any spaces before or after the equal sign. It should be like this:

bash 中的变量赋值不应在等号前后有任何空格。应该是这样的:

#!/bin/bash
var=$(cat tmp/pids/unicorn.pid)
echo "$var"

Which can be written more idiomatically as

哪个可以更惯用地写成

#!/bin/bash
var=$(< tmp/pids/unicorn.pid)
echo "$var"