ops102:bash_scripting_1
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| ops102:bash_scripting_1 [2024/11/22 21:11] – chris | ops102:bash_scripting_1 [2025/03/11 15:11] (current) – chris | ||
|---|---|---|---|
| Line 38: | Line 38: | ||
| The current date and time is: | The current date and time is: | ||
| Sat Mar 6 12:03:32 EST 2038 | Sat Mar 6 12:03:32 EST 2038 | ||
| + | |||
| + | ===== Comments ===== | ||
| + | |||
| + | A comment in a bash script starts with a sharp symbol (#) and is ignored by the shell interpreter: | ||
| + | |||
| + | < | ||
| + | # written by Jason Bourne</ | ||
| + | |||
| + | A comment may also be just one portion of a line: | ||
| + | |||
| + | < | ||
| + | |||
| + | Note that a shbang line is a comment from the point of view of the shell interpreter -- it's there for the kernel to use, not the shell! | ||
| ===== Variables ===== | ===== Variables ===== | ||
| Line 47: | Line 60: | ||
| A=5 | A=5 | ||
| B=World | B=World | ||
| + | TheNameOfTheUser=Jason | ||
| If the variable does not exist, it will be created. If it does exist, the previous value will be discarded. | If the variable does not exist, it will be created. If it does exist, the previous value will be discarded. | ||
| Line 54: | Line 68: | ||
| Unlike some computer languages such as C, variables do not need to be declared. Variables are not typed -- they may be used as strings, integers, or decimal values. | Unlike some computer languages such as C, variables do not need to be declared. Variables are not typed -- they may be used as strings, integers, or decimal values. | ||
| + | Note that there must not be a space on either side of the equal sign. | ||
| ==== Accessing a Variable ==== | ==== Accessing a Variable ==== | ||
| Line 102: | Line 117: | ||
| Hello $B | Hello $B | ||
| - | You should always double-quote variables that may contain a space in their value when using them as command arguments. | + | You should always double-quote variables that may contain a space in their value when using them as command arguments. This is especially true for filenames -- you never know when a user is going to put a space in a filename! Many scripts work find with opaque filenames (those containing no whitespace) but fail with non-opaque names. |
| - | This is especially true for filenames | + | Here is an example |
| + | |||
| + | $ touch "red maple" | ||
| + | |||
| + | $ FILE=" | ||
| + | |||
| + | $ ls $FILE | ||
| + | ls: cannot access ' | ||
| + | ls: cannot access ' | ||
| + | |||
| + | $ ls " | ||
| + | 'red maple' | ||
| === Backslashes === | === Backslashes === | ||
| Line 188: | Line 214: | ||
| $ A=`ls` | $ A=`ls` | ||
| - | This is an archaic syntax | + | This is an archaic syntax |
| ===== Arithmetic ===== | ===== Arithmetic ===== | ||
| - | Bash can perform | + | Bash can perform |
| To evaluate an arithmetic expression and return a value, use ''< | To evaluate an arithmetic expression and return a value, use ''< | ||
| Line 203: | Line 229: | ||
| 12 | 12 | ||
| $ echo $B | $ echo $B | ||
| + | 13 | ||
| Note that inside the double-parenthesis, | Note that inside the double-parenthesis, | ||
ops102/bash_scripting_1.1732309892.txt.gz · Last modified: 2024/11/22 21:11 by chris
