Single quote _error_.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Single quote _error_.
# 1  
Old 12-22-2018
Single quote _error_.

Hi all...

(This is Don's domain.)
I have come across an anomaly in sh and dash compared to bash.
It involves echoing a character set to a file in sh and dash compared to bash.
It is probably easier to show the code and results first.
Code:
#!/usr/local/bin/dash

#!/bin/sh
#!/bin/bash

echo 'ab c \n ab c \q' > /tmp/ascii.txt

hexdump -C /tmp/ascii.txt

Results using OSX 10.14.1, default bash terminal.
Code:
Last login: Sat Dec 22 14:47:39 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> # Using bash:
AMIGA:amiga~/Desktop/Code/Shell> ./ascii_test.sh
00000000  61 62 20 63 20 5c 6e 20  61 62 20 63 20 5c 71 0a  |ab c \n ab c \q.|
00000010
AMIGA:amiga~/Desktop/Code/Shell> # Using sh (bash) or dash:
AMIGA:amiga~/Desktop/Code/Shell> ./ascii_test.sh
00000000  61 62 20 63 20 0a 20 61  62 20 63 20 5c 71 0a     |ab c . ab c \q.|
0000000f
AMIGA:amiga~/Desktop/Code/Shell> _

I thought that anything inside single quotes was raw ascii text and \n and some others would be saved as such.
'bash' shows it correctly but going posix does, (in this case), a newline character sent to the file.
Am I missing something or does posix not allow raw text when backslash is used on things like '\f', (which found the problem), '\t' etc...
# 2  
Old 12-22-2018
It's not in vain that people advise to abstain from using echo because its implementation is inconsistent and differs from system to system, and also between shell versions like sh and bash.
man sh resp. man dash:
Quote:
echo [-n] args...
Print the arguments on the standard output, separated by spaces. Unless the -n option is present, a newline is output following the arguments.

If any of the following sequences of characters is encountered during output, the sequence is not output. Instead, the specified action is performed:

\b A backspace character is output.
\c Subsequent output is suppressed. This is normally used at the end of the last argument to suppress the trailing newline that echo would otherwise output.
\e Outputs an escape character (ESC).
\f Output a form feed.
\n Output a newline character.
whereas in bash (or the external /bin/echo) that behaviour needs to be switched on. man bash:
Quote:
echo [-neE] [arg ...]
Output the args, separated by spaces, followed by a newline. The return status is 0 unless a write error occurs. ... If the -e option is given, interpretation of the following backslash-escaped characters is enabled.
So - you may call that an anomaly, but it is an expected and well known one.
These 3 Users Gave Thanks to RudiC For This Post:
# 3  
Old 12-22-2018
Hi RudiC...

Thanks a lot, as soon as I read your post I realised that I had transient, (external), versions of the command(s)...
I assumed dash's and sh's version would be the same as my transient ones instead of resident, (builtin), ones...
Never make assumptions eh! I should stick to my own quotes.
OSX 10.14.1, default bash terminal calling dash...
Code:
Last login: Sat Dec 22 16:13:19 on ttys000
AMIGA:amiga~> dash
AMIGA:\u\w> printf '%s' 'ab c \f d \n ef \q' > /tmp/ascii.txt
AMIGA:\u\w> hexdump -C /tmp/ascii.txt
00000000  61 62 20 63 20 5c 66 20  64 20 5c 6e 20 65 66 20  |ab c \f d \n ef |
00000010  5c 71                                             |\q|
00000012
AMIGA:\u\w> /usr/bin/printf '%s' 'ab c \f d \n ef \q' > /tmp/ascii.txt
AMIGA:\u\w> hexdump -C /tmp/ascii.txt
00000000  61 62 20 63 20 5c 66 20  64 20 5c 6e 20 65 66 20  |ab c \f d \n ef |
00000010  5c 71                                             |\q|
00000012
AMIGA:\u\w> echo 'ab c \f d \n ef \q' > /tmp/ascii.txt
AMIGA:\u\w> hexdump -C /tmp/ascii.txt
00000000  61 62 20 63 20 0c 20 64  20 0a 20 65 66 20 5c 71  |ab c . d . ef \q|
00000010  0a                                                |.|
00000011
AMIGA:\u\w> /bin/echo 'ab c \f d \n ef \q' > /tmp/ascii.txt
AMIGA:\u\w> hexdump -C /tmp/ascii.txt
00000000  61 62 20 63 20 5c 66 20  64 20 5c 6e 20 65 66 20  |ab c \f d \n ef |
00000010  5c 71 0a                                          |\q.|
00000013
AMIGA:\u\w> _

I wasn't actually using this in any code I just wanted a raw file as a test, that is how I came across it.
Now logged in the old grey matter to use transient versions to test with too.
Looks like 'printf' is the way to go; for quickness I never even considered 'printf'...

Many thanks matey...
These 2 Users Gave Thanks to wisecracker For This Post:
# 4  
Old 12-22-2018
GNU allowed bash custom built-ins way back in early version 3 of bash, I believe. CFA Johnson has made posts on the forums here about how to do exactly that. Of course it kills portability and consistency, but if you ship an application complete with all of its requirements, kind of like the idea of a static link, then you can propagate the non-standard behavior. Maybe not a great idea, but I've seen it on homegrown HPUX 11i applications.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 12-22-2018
Expanding on what RudiC has already said and replying to wisecracker's comment that this question is in my domain... If you're trying to write portable code, DO NOT use echo if you intend to invoke it with a first argument starting with <minus-sign> (-), with -- as an argument, or if any argument contains a <backslash> character (\).

The synopsis for echo in the standards is echo [string...] and the options section for echo in the standards says:
Code:
The echo utility shall not recognize the "--" argument in the manner specified by Guideline 10
of XBD Section 12.2 (on page 216); "--" shall be recognized as a string operand.

Implementations shall not support any options.

And then, unlike the description of any other utility in the standards, the description of the string operand is:
Code:
	string	A string to be written to standard output. If the first operand is -n, or if any of the
		operands contain a <backslash> character, the results are implementation-defined.

XSI		On XSI-conformant systems, if the first operand is -n, it shall be treated as a string,
		not an option. The following character sequences shall be recognized on XSI-conformant
		systems within any of the arguments:

		\a	Write an <alert>.

		\b	Write a <backspace>.

		\c	Suppress the <newline> that otherwise follows the final argument in the
			output. All characters following the '\c' in the arguments shall be
			ignored.

		\f	Write a <form-feed>.

		\n	Write a <newline>.

		\r	Write a <carriage-return>.

		\t	Write a <tab>.

		\v	Write a <vertical-tab>.

		\\	Write a <backslash> character.

		\0num	Write an 8-bit value that is the zero, one, two, or three-digit octal number
			num.

Except for the first paragraph of the above quote, this section is shaded and has an XSI margin tag which means that it applies only on systems that support the standard's XSI option (which means that the system supports all of the optional requirements needed for UNIX brand certification by The Open Group in addition to meeting all of the requirements needed for POSIX certification by the IEEE).

Last edited by Don Cragun; 01-06-2019 at 09:56 AM.. Reason: Fix typos: s/−/-/g
These 3 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

2. UNIX for Dummies Questions & Answers

Insert single quote and commas

Hi All, I have a set of data as below : XS012371378 Raj 23-09-12 SH128238948 Andrew 24-08-12 CH273712399 Walsh 12-10-12 JK7249923893 Nick 10-02-13 JP6383791389 Braslin 30-12-13 I want the first column to be extracted separately. I can get this using awk. awk '{print $1}' file_name ... (3 Replies)
Discussion started by: Nand Kishor
3 Replies

3. Shell Programming and Scripting

How to insert a single quote to each record

I have a file as: 1 New used 1 used New I need o/p as: '1' 'New' 'used' '1' 'used' 'New' (12 Replies)
Discussion started by: karumudi7
12 Replies

4. Shell Programming and Scripting

single quote replacement

hi all, i have a data in the file which of the formate : 100,102,103 and the required formate is \'100\',\'102\',\'103 Idealy we need to replace , with \',\' Regards arkesh (2 Replies)
Discussion started by: arkeshtk
2 Replies

5. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

6. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

7. UNIX for Dummies Questions & Answers

how to print single quote in awk

Hi all, It is a very stupid problem but I am not able to find a solution to it. I am using awk to get a column from a file and I want to get the output field in between single quotes. For example, Input.txt 123 abc 321 ddff 433 dfg ........ I want output file to be as ... (6 Replies)
Discussion started by: gauravgoel
6 Replies

8. Shell Programming and Scripting

escaping single quote

hi, echo 'abc' will give output abc how can i get output as 'abc' plz help. thanks in advance (3 Replies)
Discussion started by: javeed7
3 Replies

9. Shell Programming and Scripting

single quote

Hi I have a shell script with many lines as below: comment on column dcases.proj_seq_num is dcases_1sq; .... .... I want the above script to be as below: comment on column dcases.proj_seq_num is 'dcases_1sq'; I want to have single quotes like that as above for the entire shell... (2 Replies)
Discussion started by: dreams5617
2 Replies

10. Shell Programming and Scripting

Replacing a single quote

Hi there I have a data file like so below 'A/1';'T100002';'T100002';'';'01/05/2004';'31/05/2004';'01/06/2004';'08/06/2004';'1.36';'16';'0.22';'0';'0';'1.58';'0';'0';'0';'0';'0';'0';'clientes\resumen\200405\resumen_T100002_T100002_1.pdf';'';'0001';'S';'20040501';'';'02';'0';'S';'N'... (3 Replies)
Discussion started by: rjsha1
3 Replies
Login or Register to Ask a Question