Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Can't get "echo" to work in a script!

  1. #1
    Join Date
    Feb 2009
    Beans
    5

    Red face Can't get "echo" to work in a script! [SOLVED]

    Hello,

    I'm trying to use echo in a script and it isn't working according to the man page. My demo script is as follows:
    Code:
    #! /bin/sh
    
    echo -e -n "hello\nworld\n"
    echo -n -e "hello\nworld\n"
    echo -n  "hello\nworld\n"
    echo -e  "hello\nworld\n"
    And it outputs:
    Code:
    -e -n hello
    world
    
    -e hello
    world
    hello
    world
    -e hello
    world
    According to the manpage, I would expect -e to be a valid option and to be turned off by default (both of these things seem to be false here). Furthermore if I do the same in an interactive shell I get:
    Code:
    $ echo -e -n "hello\nworld\n"
    hello
    world
    $ echo -n -e "hello\nworld\n"
    hello
    world
    $ echo -n  "hello\nworld\n"
    hello\nworld\n$ echo -e  "hello\nworld\n"
    hello
    world
    just as I would expect. Piping the commands into bash also works as I would expect.

    The command "which echo" returns "/bin/echo" from the script and from the interactive shell. I'm sure that I'm missing something here, but I can't for the life of me figure it out. Does anybody have any input? Thanks!
    Last edited by foobster; February 17th, 2009 at 08:33 AM. Reason: To say that it's been solved.

  2. #2
    Join Date
    Jan 2007
    Location
    ~/SC/USA
    Beans
    1,969
    Distro
    Ubuntu

    Re: Can't get "echo" to work in a script!

    What do you get running it in this manor?
    Code:
    echo -ne "hello\nworld\n"

  3. #3
    Join Date
    Feb 2009
    Location
    United States
    Beans
    52
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Can't get "echo" to work in a script!

    Change the #! /bin/sh to #!/bin/bash

    I then get

    hello
    world
    hello
    world
    hello\nworld\nhello
    world
    Not exactly sure why sh and bash are interpreting it differently, but it does fix it.

  4. #4
    Join Date
    Feb 2009
    Beans
    5

    Re: Can't get "echo" to work in a script!

    In the interactive shell I get what I would expect (-ne = -en = -e -n = -n -e). From the script I get:

    Code:
    -en hello
    world
    
    -ne hello
    world
    It doesn't like that e for some reason.

  5. #5
    Join Date
    Feb 2009
    Beans
    5

    Re: Can't get "echo" to work in a script!

    Thanks for the reply, I'm glad to have it working now. I'm still a little confused as to why it's like this. Also odd is that "echo --help" and "echo --version" don't work even though the man page says they should.

  6. #6
    Join Date
    Feb 2009
    Beans
    5

    Re: Can't get "echo" to work in a script!

    If anyone finds this post, here is a relevant link: https://bugs.launchpad.net/ubuntu/+s...ash/+bug/78116.

    Here is another: https://bugs.launchpad.net/ubuntu/+s...ash/+bug/61463

    The problem is just that most people are used to /bin/sh being linked to bash while in Ubuntu it is linked to dash. In dash, "-e" is not a valid option and escape characters are interpreted by default.

  7. #7
    Join Date
    Jan 2007
    Location
    ~/SC/USA
    Beans
    1,969
    Distro
    Ubuntu

    Re: Can't get "echo" to work in a script!

    Great find........Thanks.

  8. #8
    Join Date
    Feb 2009
    Beans
    5

    Re: Can't get "echo" to work in a script!

    Sorry, one more thing and I'll let this post die.

    I found this link (https://wiki.ubuntu.com/DashAsBinSh) and it does a really great job of explaining why the /bin/sh link was changed to dash and what it means. It also explains some differences in syntax that you might get caught on if you're expecting it to be bash (echo for example). Also interesting is that "~" won't expand in dash. Interesting.

  9. #9
    Join Date
    Jul 2006
    Location
    Here
    Beans
    11,187

    Re: Can't get "echo" to work in a script!

    yeah, times have changed. i no longer use "/bin/sh" in my scripts, i use "/bin/dash" or "/bin/bash" depending on the script.
    example, for something simple like a launcher:
    Code:
    #!/bin/dash
    exec xmessage " Loading..... " -center -timeout 10 -buttons , | firefox &
    i think it's better to just not assume and do it the right way.

  10. #10
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Can't get "echo" to work in a script!

    There are three echos on your system.
    1. The built-in echo in bash. Read its syntax in builtins(7) or bash(1).
    Code:
    $ bash
    $ type echo
    echo is a shell builtin
    2. The built-in echo in dash. Read its syntax in dash(1)
    Code:
    $ sh
    $ type echo
    echo is a shell builtin
    3. The separate command /bin/echo, which is the one explained by the man-page echo(1), and do accept --help and --version
    Code:
    /bin/echo --help
    /bin/echo --version

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •