User Tools

Site Tools


ops102:bash_scripting_1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ops102:bash_scripting_1 [2025/03/08 02:55] – [Setting a Variable] chrisops102: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:
 +
 +<code># test script #1
 +# written by Jason Bourne</code>
 +
 +A comment may also be just one portion of a line:
 +
 +<code>cd - # change to the user's previous working directory</code>
 +
 +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 55: 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 103: 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. 
 + 
 +Here is an example -- the difference between the first ''ls'' statement (which does not double-quote the variable) and the second ''ls'' statement (where the variable expansion is double-quoted):
  
-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.+  $ touch "red maple" 
 +   
 +  $ FILE="red maple" 
 +   
 +  $ ls $FILE 
 +  ls: cannot access 'red': No such file or directory 
 +  ls: cannot access 'maple': No such file or directory 
 +   
 +  $ ls "$FILE" 
 +  'red maple'
  
 === Backslashes === === Backslashes ===
ops102/bash_scripting_1.1741402513.txt.gz · Last modified: 2025/03/08 02:55 by chris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki