Variables
Shell variables may contain arbitrary strings, such as paths, file names, options and the like. Shell variables start with a dollar sign. Usage in the tcsh shell is as follows:
- set <name≥<string>: set the content of a script variable <name> to <string>
$
<name>: refers to content of variable <name>
A list of currently used shell variables can be printed with the command set
without arguments (or setenv
for all environment variables). For example, $$$cwd
is a shell variable which contains the path of the user’s current working directory.
The following is specific for the tcsh:
Shell variables offer various kinds of text substitutions:
$
name:s/a/b/: substitute occurrences of “a” with “b” in variable <name>$
name:r: remove file extension from file name contained in variable <name>
A common usage example of variable substitution is batch file renaming via the foreach loop of the tcsh:
foreach name (*.foo) mv $name $name:r.bar end
For a detailed explanation of the foreach command see the tcsh man page.
The following is specific for the bash:
The syntax for variables with the bash shell is the following:
- <name≥<string>: set the content of a script variable <name> to <string>
$
{name/a/b/}: substitute occurrences of “a” with “b” in variable <name>
Batch file processing example for the bash shell:
for i in *.foo; do mv $i ${i/.foo/.bar} done