如何将命令结果存储在shell变量中? [英] How to store command results in a shell variable?

查看:79
本文介绍了如何将命令结果存储在shell变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找出主目录中目录和文件的数量,并将其存储在shell变量中.我正在使用以下命令集.

I want to find out the number of directories and files in home directory and store that in a shell variable. I am using the following set of commands.

command="ls -l | grep -c \"rahul.*patle\""
eval $command

我想将结果存储在变量中.我该怎么办?

I want to store the result in a variable. How can I do this?

推荐答案

将命令输出存储到变量中的语法为 var = $(command).

The syntax to store the command output into a variable is var=$(command).

因此您可以直接这样做:

So you can directly do:

result=$(ls -l | grep -c "rahul.*patle")

变量 $ result 将包含匹配项的数量.

And the variable $result will contain the number of matches.

这篇关于如何将命令结果存储在shell变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆