如何将 Bash 命令的输出分配给变量? [英] How to assign the output of a Bash command to a variable?

查看:32
本文介绍了如何将 Bash 命令的输出分配给变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 pwd 命令的内容放入我稍后将使用的 shell 变量时遇到问题.

这是我的 shell 代码(循环不会停止):

#!/bin/bash密码=`密码`直到 [ $pwd = "/" ]做回声 $pwdls & &cd .. & &ls$pwd=`pwd`完毕

你能发现我的错误吗?

解决方案

尝试:

pwd=`pwd`

pwd=$(pwd)

<块引用>

注意等号后面没有空格.

正如韦斯先生所指出的那样;您不分配给 $pwd,而是分配给 pwd.

I have a problem putting the content of pwd command into a shell variable that I'll use later.

Here is my shell code (the loop doesn't stop):

#!/bin/bash
pwd= `pwd`
until [ $pwd = "/" ]
    do
        echo $pwd
        ls && cd .. && ls 
        $pwd= `pwd` 
    done

Could you spot my mistake, please?

解决方案

Try:

pwd=`pwd`

or

pwd=$(pwd)

Notice no spaces after the equals sign.

Also as Mr. Weiss points out; you don't assign to $pwd, you assign to pwd.

这篇关于如何将 Bash 命令的输出分配给变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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