0

I write a basic script which it has colourful lines in it.

#!/bin/bash

g="\e[32m"
_0="\e[0m"

printf "$g name $_0"

The script runs normally but at shellcheck.net I got this message: Don't use variables in the printf format string.

Another problem when I convert my script to #!/bin/sh my script doesn't run correctly, giving this output:

\e[32m name \e[0m

I used echo -e but this time sh said that -e wasn't supported. How do I write a common line of code to solve this problem? Thank you.

1 Answers1

2

You can use this, will work either on bash or sh:

#!/bin/bash

# \e not supported by POSIX; use \033 (octal) instead.
g=$(printf '\033[32m')
_0=$(printf '\033[0m')

printf '%s name %s foo' "$g" "$_0"