Project 84. A Bash and Tcsh Reference"What's the correct syntax for . . . ?" This project looks at the syntax of common shell commands such as variable assignment, redirection, and shell scripting statements. It shows the syntax for Bash and Tcshthe two shells that are used most often in Mac OS X Unix. Learn More
Learn More
Set VariablesTable 9.2 shows you how to set shell variables and environment variables.
Redirection and PipeliningTable 9.3 shows the syntax employed by both shells to express redirection and pipelining.
Learn More
Tee TimeTo see the output of a command onscreen and redirect it to a file, use the tee command. $ ls Sites | tee list.txt images index.html $ cat list.txt images index.html To redirect to multiple files, just type the names of the files as arguments. Apply option -a to append to the output files rather than overwrite them. Startup FilesThe following script files are executed by the Bash shell when it starts up. For login shells (or shells started with the command bash --login), they are
Note
Learn More
For non-login shells, they are
The following script files are executed by the Tcsh shell when it starts up. For login shells (or shells started with the command tcsh -l), they are
For non-login shells, they are
Control ConstructsSyntax for each Bash and Tcsh control construct is illustrated in the following examples. All the scripts actually work, so you can play around with them. The if Construct#!/bin/bash if [ "$1" = "positive" ]; then echo "Yes" elif [ "$1" = "negative" ]; then echo "No" else echo "Not sure" fi #!/bin/tcsh if ("$1" == "positive") then echo "Yes" else if ("$1" == "negative") then echo "No" else echo "Not sure" endif The case/switch Construct#!/bin/bash case "$1" in "positive") echo "Yes" ;; "negative") echo "No" ;; *) echo "Not sure" ;; esac #!/bin/tcsh switch ("$1") case "positive": echo "Yes" breaksw case "negative": echo "No" breaksw default: echo "Not sure" breaksw endsw Learn More
The for Loop#!/bin/bash for word in hello goodbye au-revoir; do echo $word done #!/bin/tcsh foreach word (hello goodbye au-revoir) echo $word end The while Loop#!/bin/bash n=0 while [ ! $n = 10 ]; do echo $n n=$(expr $n + 1) done #!/bin/tcsh set n = 0 while ($n != 10) echo $n set n = `expr $n + 1` end |