Pregunta

I'm trying to write a simple bash function that returns bold text. The code I have written so far is:

function txt_bold() {<br>
    echo -e '\033[1m$1\033[0m$2'<br>
    tput sgr0<br>
}

When I write txt_bold "This is bold" "And this in plain text" it returns "$1$2" ($1 in bold). What am I doing wrong here?

¿Fue útil?

Solución

Use " instead of '.

function txt_bold() {
  echo -e "\033[1m$1\033[0m$2"
  tput sgr0
}

Short

Within single quotes variables are not getting expanded.

Long

Below's the bottom line of this article, which might help you understand it: What’s the Difference Between Single and Double Quotes in the Bash Shell?

Double Quotes

  • Use when you want to enclose variables or use shell expansion inside a string.
  • All characters within are interpreted as regular characters except for $ or ` which will be expanded on the shell.

Single Quotes

  • All characters within single quotes are interpreted as a string character.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top