Problem with echo` -e`

Asked

Viewed 175 times

4

I’m starting to learn shell script and am doing some simple scripts to train. The script below tests whether whoever is 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 and 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 the -e as argument and print on screen that:

-e
You are the superuser

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

Sorry for the accents, my keyboard is not yet configured.

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 the # and the ! everything will work. With this space the comment is only a comment, not which program should 'run' your script. What is executed there is the standard shell, the sh, and the echo it has no option -e. Therefore, it prints -e and then the phrase.

  • 1

    Perfect! It worked. Thank you very much, it was worth taking this doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.