#!/bin/bash # This is our first script. echo 'Hello World!' Usually has some variation of the “shebang” #!/bin/bash
Below is an often used alternative to the above. In production scripts it’s probably better to use this variation.
#!/usr/bin/env bash
Getting Started # Bash Script Header Making Scripts Executable Putting Scripts in Your Path Style and Design Strategy # Bash Scripting Style Indentation in Bash Top-Down Design Useful Helpers # ShellCheck - just use it always Boilerplate Pure Bash Bible Variables and Constants # Variables in Bash Scoping in Bash Redirection # Here Documents 20220406080732-here-strings Functions # Functions in Bash Flow Control # if/then Constructs test Builtin Extended test File Tests Integer Comparison String Comparison Logical Operators Control Operators Looping # while until File-Redirection and Looping for Loops for: C Language Form Branching # case Exit Status # Exit Status Interactivity # read Builtin Input Validation Menus IFS # Internal Field Separator Troubleshooting # Troubleshooting Strategies Defensive Programming Verify Input Testing Debugging See also # Use Bash Strict Mode Parameters # Positional Parameters Special Parameters Basic Parameters Parameter Expansion # Parameter Expansion Manage Empty Variables Variable Name Expansion String Operations Case Modification Arithmetic Evaluation and Expansion # Arithmetic Evaluation Arithmetic Expressions Operators # Unary Operators Arithmetic Operators Assignment and Increment/Decrement Operators Bit Operations Logical and Comparison Operators Advanced Arithmetic in the Shell # See: bc Arrays # Arrays Creating Arrays Array Indexing Array Operations Associative Arrays Grouping Commands # Grouping Commands Process Substitution Traps # trap Builtin Temporary Files # Temporary Files Asynchronous Execution # wait Builtin Named Pipes # Named Pipes
Write scripts easy to read and maintain.
Resources # Google - Shell Style Guide It’s easy to understate how useful this style-guide is. It’s basically become my style bible as of late. This should really be the defacto standard. Use long option names to improve readability # [me@linuxbox ~]$ ls -ad # is equivalent to: [me@linuxbox ~]$ ls --all --directory
$_ - Does some things at shell startup, but most useful because it expands to the last argument to the previous simple command.
i[akraker@localhost wiki]$ ls -lah ~/bash total 4.0K drwxrwxr-x. 1 akraker akraker 36 Sep 18 06:44 . drwx------. 1 akraker akraker 1.4K Sep 18 06:40 .. -rwxrwxr-x. 1 akraker akraker 169 Sep 18 06:44 countparameters.sh i[akraker@localhost wiki]$ echo $_ /home/akraker/bash Note ~ expansion occurred before _ variable was set.
...
$a or ${a} Is required when variable is next to other text which may confuse the shell.
Work at the bit level. For certain low-level tasks.
Operator Description ~ Bitwise negation. Negate all the bits in a number. << Left bitwise shift. Shift all bits in a number to the left. >> Right bitwise shift. Shift all bits in a number to the right. & Bitwise AND. Perform AND operation on all bits in two numbers.
...
case case word in [pattern [| pattern]…) commands ;;]… esac
#!/bin/bash # case-menu: a menu driven system information program clear echo " Please Select: 1. Display System Information 2. Display Disk Space 3. Display Home Space Utilization 0. Quit " read -p "Enter selection [0-3] > " case "$REPLY" in 0) echo "Program terminated." exit ;; 1) echo "Hostname: $HOSTNAME" uptime ;; 2) df -h ;; 3) if [[ "$(id -u)" -eq 0 ]]; then echo "Home Space Utilization (All Users)" du -sh /home/* else echo "Home Space Utilization ($USER)" du -sh "$HOME" fi ;; *) echo "Invalid entry" >&2 exit 1 ;; esac Note: can use patterns just like in pathname expansion
...
Good for normalizing input. declare -u upper-case declare -l lower-case
Format Result ${parameter,,pattern} Expand the value of parameter into all lowercase. pattern is optional. ${parameter,pattern} Expand value of parameter, changing only first character to lowercase. ${parameter^pattern} Expand value of parameter into all uppercase ${parameterpattern} Expand value of parameter, changing only first character to uppercase.
...
# arithmetic true/false [me@linuxbox ~]$ if ((1)); then echo "true"; else echo "false"; fi true [me@linuxbox ~]$ if ((0)); then echo "true"; else echo "false"; fi false # ternary operator example: "toggle" [me@linuxbox ~]$ a=0 [me@linuxbox ~]$ ((a<1?++a:--a)) [me@linuxbox ~]$ echo $a 1 [me@linuxbox ~]$ ((a<1?++a:--a)) [me@linuxbox ~]$ echo $a 0
&& (AND) || (OR) command1 && command2 If command1 is successful execute command2
command1 || command2 If command1 is unsuccessful execute command2