1

Below is a csh script.

#! /bin/csh
set alpha=10\20\30;
set beta = $alpha.alpha;
perl -p -i.bak -e 's/gamma/'$beta'/' tmp;

The tmp file contains just the word gamma. After running tmp.csh, I expect 10\20\30.alpha in tmp, but it's now 102030.alpha.

How to preserve slashes in this situation?

Note: I wouldn't prefer changing definition of alpha variable, as it is used in the script else where where it needs to be in this format (10\20\30) only.

Thanks.

1 Answer 1

2

In csh, for your alpha assignment, the backslash is being taken to mean 'a literal 2 or 3'. In order to keep csh from doing this, the assignment needs to be enclosed in quotes.

#! /bin/csh
set alpha="10\20\30";
set beta = $alpha.alpha;
perl -p -i.bak -e 's/gamma/'$beta'/' tmp;

If in doubt, it's often helpful to 'echo' your variables out to see exactly what they contain. I don't understand your final note, as the 'alpha' variable is not equal to 10\20\30 the way you have it originally assigned.

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

3 Comments

Hello, this works, but what if I have forward slashes in alpha? It gives another error then.
Did you mean to use the control characters from '\2' or '\3' or the sequence of a '\' followed by a '2' or '3'? If it is the former, then the backslashes in alpha will need to be escaped (backslashed) themselves. Like 'set alpha="10\\20\\30";'
I found other alternative for forward slash case. instead of 's/gamma/'$beta'/' I use 's@gamma@'$beta'@'. This way it doesn't confuse forward slashes in $beta with substitution operator delimiters. Thanks for your help anyway.

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.