Keep spaces with read command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Keep spaces with read command
# 1  
Old 03-22-2005
Keep spaces with read command

Hi

The following command read a string from the keyboard & echo it back.
$ read S;echo "$S"
ABCD
ABCD

As you see, the input has space. while it echo back the spaces are removed. Is there a way to keep the input intact and not removing any spaces? Also, the number of spaces may vary.

Thanks
# 2  
Old 03-22-2005
IFS="" read S;echo "$S"
# 3  
Old 03-22-2005
Thanks! It works fine on the prompt. How can I put it in the script. I've a script which reads each line from the input

#!/bin/ksh
{ while read myline
do
echo $myline
done
} < filename
# 4  
Old 03-22-2005
Its working now!

#!/bin/ksh
{ while IFS="";read myline
do
echo $myline
done
} < filename

Thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - read white spaces

Hello! I have one problem with my bash script - I would like to be able to read white space characters from stdin (for example single " ") - can I acomplish that somehow? I need to read only one character at the time, so I use read -s -n 1 var but it doesn't work for whitespaces apparently. ... (3 Replies)
Discussion started by: xqwzts
3 Replies

2. Shell Programming and Scripting

The "read" command misinterprets file names containing spaces

The "read" command, which is built into bash, takes words from the standard input. However, "read" is not good at taking file names if the file names contain spaces. I would like my bash script to ask the user to enter file names, which may contain spaces. Can you think about any technique for... (14 Replies)
Discussion started by: LessNux
14 Replies

3. Shell Programming and Scripting

Having a for loop read in lines with spaces?

Is this possible? I have a for loop in a shell script reading a list, but I want each line to be a loop, not each thing with a space. Here is the example: HOSTLIST="\ 1.2.3.4 serverA 1.2.3.5 serverB" for NBUHOST in `echo $HOSTLIST` do ssh ${SERVERNAME} "echo "${NBUHOST}"... (3 Replies)
Discussion started by: LordJezoX
3 Replies

4. Shell Programming and Scripting

how to read blank spaces

hi i have a file which store some data.the contents of my file is data1:data2 data3:data4 i have a script which read this file correct="$(cat /root/sh | cut -d: -f1)" i used this syntax..please help me which syntax is used to read blank spaces.and then remove it and after that how to read... (1 Reply)
Discussion started by: shubhig15
1 Replies

5. UNIX for Dummies Questions & Answers

How to read files with spaces

Hi I am a newbie to unix. I have a current script that reads a directory for excel files and renames the files. There is a problem now because some of the files have spaces. If I put quotes on the file, it will work but I dont know how to read all the files with quotes. Variables $1 =... (6 Replies)
Discussion started by: Lillyt
6 Replies

6. Shell Programming and Scripting

Read file line spaces

I have a script which read a file it does while read -r line, then i echo line out echo "$line" Problem is the echo does not echo space and tabs at the end of each line. How do i get the end of line space as well (6 Replies)
Discussion started by: kelseyh
6 Replies

7. Shell Programming and Scripting

"read" command ignoring leading spaces

I have to read a file line by line, change it and then update the file. Problem is, when i read the file, "read" command ignores leading spaces. The file is a script which is indented in many places for clarity. How to i make "read" command read leading spaces as well. (3 Replies)
Discussion started by: vickylife
3 Replies

8. Shell Programming and Scripting

Read variables contain spaces from text file

Dears, I developed a shell script to read varibales from text file as the following: cat /dev/null > /rename-OUT.txt while read line do set -- `echo $line` snmpset -c dslmibs $1 sysName.0 octetstring $2 after=$(snmpget -c dslmibs $1 sysName.0 | cut -d: -f3) echo "$1,$2,$after" >>... (1 Reply)
Discussion started by: ahmed.zaher
1 Replies

9. Shell Programming and Scripting

How to read a line when it starts with spaces

Hi , I use read command to get the input text, When i try to get the line starting with spaces or ending with spaces it automatically truncates the spaces and displays the remaining content. Code i tried (UserInput.sh): #!/bin/bash echo -n "Enter some text > " read text echo "You... (3 Replies)
Discussion started by: PrakashChinna
3 Replies

10. Shell Programming and Scripting

Read files including spaces

I am accessing two files. I am using read command to read from the files. For the first file, I need read the fields delimited by spaces, and for the other file, I need to read the whole line as a single field including the spaces. When I used read command for the second file, the spaces... (4 Replies)
Discussion started by: kumariak
4 Replies
Login or Register to Ask a Question