bash -c变量未分配 [英] bash -c variable does not get assigned

查看:81
本文介绍了bash -c变量未分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下命令:

I am trying to execute the following command:

 $ bash -c "var='test' && echo $var"

,仅打印一个空行.

如果我在没有bash -c

$ var='test' && echo $var 
test

正在打印分配给$var的值.有人可以解释为什么我不能在第一个示例中分配变量吗?

the value assigned to $var is being printed. Could someone explain why I can't assign variables in the first example?

推荐答案

双引号可扩展变量,因此您的命令将扩展为

Double quotes expand variables, so your command is expanded to

bash -c "var='test' && echo"

如果$var为空,则在运行它时.您可以使用

if $var is empty when you run it. You can verify the behaviour with

var=hey
bash -c "var='test' && echo $var"

切换引号:

bash -c 'var="test" && echo $var'

这篇关于bash -c变量未分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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