C Shell Scripting/Variables
Using variables in a script
set command will save values in C shell that can be used later. When you reference the variable later you must precede the variable name with a dollar sign. When assigning values that have spaces surround the value with quotes to save as a single value or use parentheses to store the individual values.
For example, this script
#!/bin/csh -f set greetingA = Good Morning set greetingB = "Good Morning" set greetingC = (Good Morning) echo $#greetingA echo $#greetingB echo $#greetingC echo $greetingA echo $greetingB echo $greetingC echo $?greetingA echo $?greetinga echo $?greetingD
will print the following:
1 1 2 Good Good Morning Good Morning 1 0 0
The $#variable will print out the number of word in the list. And $?variable will return whether it exists. Also, variables names are case sensitive.
- All variables begin with a dollar sign ("$") when you actually use but don't when you assign them.
- Assigning variable require using a
setcommand similar to BASIC languages.
- Quoting when assigning a variable is required to store a value with spaces.