Find the answer to your Linux question:
Results 1 to 10 of 10
I have entered something like this in a bash script: printf "This prints two strings in single quotes '%s, %s!'\n" two strings This works as I expect. If I do ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    9

    printf in bash


    I have entered something like this in a bash script:

    printf "This prints two strings in single quotes '%s, %s!'\n" two strings

    This works as I expect.

    If I do the same from the shell command line I receive the error message :

    bash: !\'\n": event not found

    It seems the shell sees the '!' character as a history command when on the command line but not from within the script. Why is this?

    Thanks

  2. #2
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Please read the documentation about bash quotation (i.e. how they are interpreted by the shell).

  3. #3
    Just Joined!
    Join Date
    Aug 2010
    Posts
    9

    printf in bash

    Thank you for your reply.

    The issue I have is that the command behaves differently in a script than it does on the command line. As the same shell program (as another shell process) is running the script as the command line (login shell), why do I not get the same result?

    I can make the command line work by escaping the '!' character, like this: \!, but why do I have to do this on the command line and not in the script?

    Thanks

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    I suppose it is affected by the history that a script doesn't have. Usually a !XXX would search the history for XXX and execute it again.

    Beginners tutorials:
    http://cs.gettysburg.edu/~tneller/de...-tutorial.html
    BASH Help - A Bash Tutorial
    Beginner's Bash | Malta Linux User Group (It's a little old, but still useful)
    Alien's Bash Tutorial by Billy Wideling (I personally like this one)
    Code:
    man bash

  6. #5
    Just Joined!
    Join Date
    Aug 2010
    Posts
    9

    printf in bash

    Thank you for the the links. I will check these out.

  7. #6
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.
    Quote Originally Posted by jmvalks View Post
    ... I can make the command line work by escaping the '!' character, like this: \!, but why do I have to do this on the command line and not in the script? ...
    HISTORY EXPANSION
    The shell supports a history expansion feature that is similar to the
    history expansion in csh. This section describes what syntax features
    are available. This feature is enabled by default for interactive
    shells, and can be disabled using the +H option to the set builtin com-
    mand (see SHELL BUILTIN COMMANDS below). Non-interactive shells do not
    perform history expansion by default.

    -- excerpt from man bash
    The man page for bash is long, but usually has the answers ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  8. #7
    Just Joined!
    Join Date
    Aug 2010
    Posts
    9

    printf in bash

    Thank you for your answer. I might have got there eventually by reading the various manuals but your extract from the bash manual does look like the definitive word.

    Is it true then that the quoting rules work the same within a script as they do from the command line, e.g. instructions to sed and awk as well as to printf need to be quoted no matter what?

    Thanks again.

  9. #8
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.

    The shell doesn't care what the command actually is. You will need to use quotes to make certain characters lose their special meaning to the shell.

    For example the command:
    Code:
    sed s/mjvalks/jmvalks/ file
    will not need quotes because there are no characters involved that have special meaning to the shell.

    Personally, I rarely use complex commands involving quotes interactively because I'm not a good typist. If I think I'll need a command more than twice, I'll just put it into a shell script so that I know it will be executed consistently.

    See sections on quoting in the tutorials ... cheers, drl
    Last edited by drl; 08-26-2010 at 02:36 PM.
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  10. #9
    Just Joined!
    Join Date
    Aug 2010
    Posts
    9

    printf in bash

    Thanks for your comments.

    One day I will read the bash manual all the way through.

    Regards

  11. #10
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    This could take some year, but it will make you wiser. Anyway, it is enough reading the parts that are of interest for a specific task. Usually you'll find all information you need in the manual pages (man) or documentation pages (info) or somewhere across the internet. Solutionsn to specific issues surely are not listed, but who could enumerate all possible combinations of usages.

Posting Permissions

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