Author: Stan Eisenstat
Subject: Re: [Cs223] How to deal with backslash?
Date: Wednesday, 19 Feb 2020, 20:29:00
> Message Posted By: Unknown
>
> I'm confused about how should we deal with backslash. The output of "echo
> a | /c/cs223/Hwk3/strwrs -q \a ab" and "echo a | /c/cs223/Hwk3/strwrs -q
> /a ab" are both ab. So my interpretation is backslash works similarly with
> forward slash by escaping the following character.
The output from
% echo A | /c/cs223/Hwk3/strwrs -q /A AB
is AB because STRWRS treats /A as A, which it replaces
by AB. However, the output from
% echo A | /c/cs223/Hwk3/strwrs -q \A AB
is AB because the SHELL converts \A to A before running
strwrs (i.e., argv[1] = "A"). Finally, the output from
% echo A | /c/cs223/Hwk3/strwrs -q '\A' AB
is A because the first argment now begins with a
backslash (argv[1] = "\\A"), which does not match any
character in the input, so no substitution is made.
(You can see this more clearly using the -v flag.)
=====
> However, the output of "echo + | /c/cs223/Hwk3/strwrs -q /+ ab" is ab but
> the output of "echo + | /c/cs223/Hwk3/strwrs -q \+ ab" is ab+. I
> understand the "/+" case but I don't know how to interpret the output from
> "\+" case. Thank you.
The output from
% echo + | /c/cs223/Hwk3/strwrs -q /+ AB
is AB because STRWRS treats /+ as a + character, not
something that matches the start of the input line, and
replaces the + in the input with AB. The output from
% echo + | /c/cs223/Hwk3/strwrs -q \+ AB
is AB+ because the SHELL converts \+ to + before running
strwrs (i.e., argv[1] = "+"), and strwrs treats the + as
matching the start of the string. (Again, using the -v
flag makes this clearer.)
--Stan-
PREV
INDEX
NEXT