Problem with 'echo -e'

4

I'm starting to learn shell script and am doing some simple scripts to train. The script below tests whether anyone running the script is logged in as root.

# !/bin/bash
# 
# This script test if you are the superuser

if [ "$(id -u)" = "0" ]; then 
    echo -e "\nYou are the superuser\n"
    exit 0  
else
    echo -e "\nYou must be the superuser to run this script\n"
    exit 1
fi

The result is the following:

rafa@ubuntu:~/Desktop/shell$ ./superuser.sh 

You must be the superuser to run this script

rafa@ubuntu:~/Desktop/shell$ sudo ./superuser.sh 
-e 
You are the superuser

rafa@ubuntu:~/Desktop/shell$ 

The problem is that when I try to run as root using the command sudo , the command echo does not understand -e as argument and prints on the screen this:

-e
You are the superuser

Yes I know, it's a stupid problem but I'm curious to know why this happens. Any idea? How can I fix it?

I'm sorry for the accents, my keyboard is not yet set up.

    
asked by anonymous 14.08.2014 / 01:45

1 answer

2

The problem is not with your script, it is perfect:)

The problem is only with your shebang :

# !/bin/bash

If you delete the space between # and ! everything will work. With this space the comment is just a comment, not the program that should 'run' your script. What is executed there and the default shell, sh , and echo does not have -e option. Therefore, it prints -e and then the phrase.

    
14.08.2014 / 01:53