How to redirect grep output to a variable?

怎甘沉沦 提交于 2020-01-11 09:30:11

问题


I have some pipe. For example, I have this pipe:

user@user:~$ cal | head -1 | grep -oP "[A-Za-z]+"

For this pipe I get this result:

September

I want to store this result to a variable. I write the following commands:

user@user:~$ cal | head -1 | month=$(grep -oP "[A-Za-z]+") | echo $month

And I get the blank string. What is the problem?


回答1:


 month=$(cal | head -1 | grep -oP "[A-Za-z]+")

or

 month=$(date +%B)


来源:https://stackoverflow.com/questions/25710640/how-to-redirect-grep-output-to-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!