如何从 Bash 函数返回字符串值 [英] How to return a string value from a Bash function

查看:30
本文介绍了如何从 Bash 函数返回字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Bash 函数返回一个字符串.

I'd like to return a string from a Bash function.

我将用 java 编写示例来展示我想要做什么:

I'll write the example in java to show what I'd like to do:

public String getSomeString() {
  return "tadaa";
}

String variable = getSomeString();

下面的例子适用于 bash,但有没有更好的方法来做到这一点?

The example below works in bash, but is there a better way to do this?

function getSomeString {
   echo "tadaa"
}

VARIABLE=$(getSomeString)

推荐答案

我知道没有比这更好的方法了.Bash 只知道写入标准输出的状态代码(整数)和字符串.

There is no better way I know of. Bash knows only status codes (integers) and strings written to the stdout.

这篇关于如何从 Bash 函数返回字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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