1
Question Body:

What's going on here?

printf.sh:

#! /bin/sh
NAME="George W. Bush"
printf "Hello, %s\n" $NAME

Command line session:

$ ./printf.sh
Hello, George
Hello, W.
Hello, Bush

UPDATE: printf "Hello, %s\n" "$NAME" works. For why I'm not using echo, consider

echo.sh:

#! /bin/sh
FILE="C:\tmp"
echo "Filename: $FILE"

Command-line:

$ ./echo.sh
Filename: C:    mp

The POSIX spec for echo says, "New applications are encouraged to use printf instead of echo" (for this and other reasons).

Edit
avatar
asked15 years ago

    Leave a Comment

    [named hyperlinks] (https://example.com)
    **bold**
    _italic_

    4 Answer

    0

    If you want all of those words to be printed out on their own, use print instead of printf

    printf takes the formatting specification and applies it to each argument that you pass in. Since you have three arguments {George, W., Bush}, it outputs the string three times using the different arguments.

    Edit
    avatar
    answered15 years ago
    0

    is there a specific reason you are using printf or would echo work for you as well?

    NAME="George W. Bush"
    echo "Hello, "$NAME
    

    results in

    Hello, George W. Bush
    

    edit: The reason it is iterating over "George W. Bush" is because the bourne shell is space delimitted. To keep using printf you have to put $NAME in double quotes

    printf "Hello, %s\n" "$NAME"
    
    Edit
    avatar
    answered15 years ago
    0

    Your NAME variable is being substituted like this:

    printf "Hello, %s\n" George W. Bush
    

    Use this:

    #! /bin/sh
    NAME="George W. Bush"
    printf "Hello, %s\n" "$NAME"
    
    Edit
    avatar
    answered15 years ago
    0

    The way I interpret the man page is it considers the string you pass it to be an argument; if your string has spaces it thinks you are passing multiple arguments. I believe ColinYounger is correct by surrounding the variable with quotes, which forces the shell to interpret the string as a single argument.

    An alternative might be to let printf expand the variable:

    printf "Hello, $NAME."
    

    The links are for bash, but I am pretty sure the same holds for sh.

    Edit
    avatar
    answered15 years ago

    Your Answer

    Drop files here or click to upload.
    undraw-questions

    Disilab for your Teams

    – Collaborate and share knowledge with a private group.

    Create a free Team