Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have written a small script as follows

cat test |while read line1
echo "$line1"

The input file "test" has the following lines

Kensington K64391US C\i70 Wireless
Desktop Nintendo Wii Wireless Nunchuck
\M470 DeLonghi HHP1500 Mica Panel
Vi\20 Radiator Heater

But "\" is missing in the output which is as follows

Kensington K64391US Ci70 Wireless
Desktop Nintendo Wii Wireless Nunchuck
M470 DeLonghi HHP1500 Mica Panel Vi20
Radiator Heater

How can I get the "\" to come in the output?

share|improve this question
It's not necessary to use cat, just redirect the file into the done like this: while ... done < test – Dennis Williamson Dec 28 '09 at 18:15

Use the -r option of read.

From the ksh man page:

In raw mode, -r, the \ character is not treated specially.

share|improve this answer

Well, a quick and easy answer is to replace all the single slashes in your input file with double slashes. This will result in a single slash in the output. (eg: in vi, use %s/\\/\\\\/g

Reasoning: The backslash is an escape character. It's used to indicate "the next character should be treated literally", to avoid things like asterisks being taken as anything but an asterisk. So putting two of them in a row results in the first one indicating that the second should be used as just a slash.

Is this viable for you, or do you need to make changes to the script itself, without touching the data?

share|improve this answer

Use -r on the read:

cat test.input | while read -r 'line'
echo "$line"
share|improve this answer
By using the -e option of echo, you could be throwing away the \ that you saved by using read -r. Say, for example, your model number was "abc\033". Just use echo without the -e. Yay! We can edit comments! – Dennis Williamson Dec 28 '09 at 18:28
-r option works. Thanks a lot – Ashu Dec 28 '09 at 18:31
Removed the -e as per Dennis comment. – davey Dec 28 '09 at 18:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.