User Tools

Site Tools


bash

Differences

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

Link to this comparison view

Next revision
Previous revision
bash [2020/08/12 17:14] – created shawnbash [2020/08/12 19:07] (current) – [Functions] Adds tips shawn
Line 1: Line 1:
 +
 +''#!/usr/bin/env bash'' - Use ''bash'' found in the environment path. Protects against not knowing where user's ''bash'' is installed.
  
 ====== set options ====== ====== set options ======
 +
 +''set options -xeuo''
  
 Commonly used options: Commonly used options:
  
-  * -x:  turns on debug output, logging each command as it is executed +  * ''-x'':  turns on debug output, logging each command as it is executed 
-  * -e:  causes the script to exit on most command failures (non-zero exit codes) +  * ''-e'':  causes the script to exit on most command failures (non-zero exit codes) 
-  * -u:  treats unset variables and parameters other than the special parameters @ or * as an error when performing parameter expansion +  * ''-u'':  treats unset variables and parameters other than the special parameters @ or * as an error when performing parameter expansion 
-  * -o pipefail: causes the script to exit if a command in a pipeline fails +  * ''-o pipefail'': causes the script to exit if a command in a pipeline fails 
-  * use a+ to unset them if necessary+  * use a + to unset them if necessary
  
 Many more options available:  https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html Many more options available:  https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
Line 14: Line 18:
 ====== Variables ====== ====== Variables ======
  
-  * $@ - All of the arguments as individual strings +  * ''$@'' - All of the arguments as individual strings 
-  * "$*" - All of the arguments as a single string +  * ''"$*"'' - All of the arguments as a single string 
-  * $# - The number of arguments +  * ''$#'' - The number of arguments 
-  * $1..n - Where n is an integer, the nth argument in the list +  * ''$1..n'' - Where n is an integer, the nth argument in the list 
-  * $0 - The command name +  * ''$0'' - The command name 
-  * $? - The last exit code +  * ''$?'' - The last exit code 
-  * $IFS - Internal/input field separator (defaults to combo of space, newline and tab)+  * ''$IFS'' - Internal/input field separator (defaults to combo of space, newline and tab) 
 + 
 +====== Functions ====== 
 + 
 +Declaring a function (needs to happen before use): 
 + 
 +''function <function_name> { ... do things ... }'' 
 + 
 +- or - 
 + 
 +''<function_name>() { ... do things ... }'' 
 + 
 +  * Nothing ever goes inside parentheses. 
 +  * Passed parameters can be accessed with ''$1''..''$n''.
  
 +====== Tips ======
  
 +  * [[https://stackoverflow.com/a/16496491|Use getopts to parse commandline options]].
bash.1597252487.txt.gz · Last modified: 2020/08/12 17:14 by shawn

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki