I have a script where the user should be able to enter a string with spaces. So far I have:
#bin/csh echo 'TEST 1' echo -n 'Input : ' set TEST = $< echo 'Var | ' $TEST set TEST=`echo $TEST` echo 'Var after echo | ' $TEST set TEST=`echo $TEST | sed 's/ /_/g'` echo 'Var after change | ' $TEST
If I enter the string ‘r r r’ at ‘input’, $TEST would only take ‘r’. I want to be able to set $TEST to ‘r r r’. Is this possible? If I enter a string like ‘1 1 1’ I get an error:
set: Variable name must begin with a letter.
What’s the reason for this?
It’s because you’re not using quotes in your
SETstatement. When you enter'r r r'as your input, the two different variants (unquoted and quoted) are equivalent to:The first of those simply sets
TESTto'r'andrto''(twice!). The second setsTESTto'r r r'. That’s becausecshlets you do multiple assignments like:So you need to use the quoted variant of
SET. Examine the following transcript to see how it works:The reason you’re getting the error you describe with input of
'1 1 1'is because you’re effectively executing:and
cshis taking this to mean that you want to create the variableTESTset to'1'followed by the variable1, which doesn’t start with a letter, hence not allowed. With the quoted variant, this becomes:which will do what you expect.