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 ...
- 08-25-2010 #1Just 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
- 08-25-2010 #2
Please read the documentation about bash quotation (i.e. how they are interpreted by the shell).
- 08-25-2010 #3Just 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
- 08-26-2010 #4
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
- 08-26-2010 #5Just Joined!
- Join Date
- Aug 2010
- Posts
- 9
printf in bash
Thank you for the the links. I will check these out.
- 08-26-2010 #6Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
The man page for bash is long, but usually has the answers ... cheers, drlHISTORY 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 bashWelcome - 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 )
- 08-26-2010 #7Just 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.
- 08-26-2010 #8Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
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:
will not need quotes because there are no characters involved that have special meaning to the shell.Code:sed s/mjvalks/jmvalks/ file
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, drlLast edited by drl; 08-26-2010 at 01: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 )
- 08-26-2010 #9Just 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
- 08-27-2010 #10
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.


Reply With Quote
