首页 > 解决方案 > 回显一个带有变量的字符串,该变量也是一个字符串和变量,并且为空白但不起作用

问题描述

我正在构建一个应该用正确的颜色回显消息的函数,但在我的代码中似乎不起作用。

代码如下:

#!/bin/bash
function green() {
  echo -e "\033[32m[ $1 ]\033[0m"
}


function software_is_installed() {
  local message="software is installed with name:"$1
  blue $message
}

green ssh

输出不是预期的,但是

[ software ]

但在另一个代码片段中它工作正常。代码如下:

#!/bin/bash
function blue() {
  echo -e "\033[34m[ $1 ]\033[0m"
}

function file_or_dir_already_exists() {
  blue "file or directory: $1 already exists"
}

file_or_dir_already_exists ~/

它按预期输出,输出如下

[ file or directory: ~/ already exists ]

我期待类似的东西

software is installed with name: ssh

标签: shell

解决方案


尝试像这样修改:

#!/bin/bash
function green() {
  echo -e "\033[32m[ $1 ]\033[0m"
}


function software_is_installed() {
  green "software is installed with name: $1"
}

software_is_installed ssh

推荐阅读