2

I am using following shebang in my script:

#!/bin/sh 

To check whether I haven't used any bash syntax, I checked my script with checkbashisms script and it says

possible bashism in my_script.sh line 3 (echo -e): echo -e "hello world" 

And I am running this script on embedded board with busybox shell.

How to resolve this?

4
  • You need to provide the relevant parts of the code. Do you need the -e flag or not? We cannot know that without seeing some code. Commented May 16, 2016 at 7:47
  • 1
    The -e option is for echo to parse escape code (used for example for VT100 escape codes or embedded newlines etc.). If your string doesn't have any escape code, then you don't need it. Commented May 16, 2016 at 7:51
  • 1
    Also, the -e option is not in the POSIX standard for echo. It's less of a "Bashism" and more of a GNUism" I would say, especially since in Linux it's not only a Bash built-in command, but also part of GNU coreutils. Commented May 16, 2016 at 7:58
  • If you are on Linux (or, more generally, on a system where /usr/bin/echo supports -e), you can write command echo -e .... The command command, which is part of the POSIX standard, causes the echo from your PATH to be executed, instead of the builtin echo. Commented May 17, 2016 at 5:27

2 Answers 2

3

POSIX allows echo -e as the default behavior. You may do

var="string with escape\nsequence" printf "%s\n" "$var" # not interpreting backslashes printf "%b\n" "$var" # interpreting backslashes 

This should pass the checkbashisms test

Sign up to request clarification or add additional context in comments.

Comments

2
help echo 
-e enable interpretation of the following backslash escapes ... 

If your code doesn't need to interpret backslash escapes then you can safely remove the -e flag.

Sometimes, people tend to use -e just for interpreting \n as newline. If that's the case, know that you can have multiline strings just by quoting them and writing as it is. An example:

var="Hi, I am a multiline string" 

But if you do need to interpret backslash escapes, then using printf will be your best bet.

Though I would recommend using printf whether you need backslash interpretation or not.

With printf:

printf "%s\n" "$var" # no backslash interpretation printf "$var" # backslash interpretation occurs 

1 Comment

echo is a shell builtin. While the man pages might correctly describe it on a GNU system, they are probably for /bin/echo which (in theory) could be different. On the version of UNIX I am running on, the shell built-in supports -e but /bin/echo does not, and the man pages do not have the line you show, however help echo does. There can be similar issues with printf, being both a shell built-in and a program in (on my system) /usr/bin.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.