4

I am using Bash script to read line by line from a text file, which has special characters in it (regular expression). When I use echo "${SOME_VAR}" it does not display the text as is.

I am familiar with Prevent * to be expanded in the bash script.

How can I display and use the text as is?

UPDATE The text (TSV) file holds tuples similar to (the last entry is a psql query)

bathroom    bathroom    select name from photos where name ~* '\mbathroom((s)?|(''s)?)\M';

I am reading the CSV as follows:

tail -n+2 text.file | while IFS=$'\t' read x y z
do
    echo "${z}"
done

which gives the output

select name from photos where name ~* 'mbathroom((s)?|(''s)?)M');

note that the '\' is missing

2
  • 2
    show us your input and some code please Commented Aug 29, 2012 at 9:07
  • An alternative to the tail process (and avoiding the pipe which may cause subshell issues) would be { read; read; while IFS=$'\t' read z y z; do ... ; done; } < text.file. Note the two throwaway read commands prior to the loop; all 3 share the redirected input to the command group. Commented Aug 29, 2012 at 12:15

1 Answer 1

3

Try using the -r flag with read:

tail -n+2 text.file | while IFS=$'\t' read -r x y z
do
    echo "${z}"
done

From the read man page:

-r Do not treat a backslash character in any special way. Consider each backslash to be part of the input line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.