C Shell Scripting/Variables

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Using variables in a script[edit | edit source]

The 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.

Lessons[edit | edit source]

  1. All variables begin with a dollar sign ("$") when you actually use but don't when you assign them.
  2. Assigning variable require using a set command similar to BASIC languages.
  3. Quoting when assigning a variable is required to store a value with spaces.