Wiki
  • Introduction
  • Inspirations
  • Software
    • Systems Design
    • Tooling
    • npm
    • CLI
    • Programming Languages
    • Terminal
    • Shell Scripting
    • macOS
    • Networking
      • TCP
      • WebSockets
      • Web RTC
    • Web Development
      • React
      • HTML / CSS
    • Backups
    • Unix
    • Observability / Monitoring
    • SRE
    • Vim
  • Web Design
    • Inspirations
  • Project Management
    • Lean
  • Career
    • Job Hunting
  • Finances
    • Car Buying
  • Clothing
    • Material
    • Mid Layers and Jackets
    • Base Layers
    • Shoes
    • Socks
  • Productivity
    • Goals
    • Learning
    • Computer Usage
  • Well Being
    • Social Bonding
    • Gratitude
    • Health
      • Injury
      • Water Fluoridation
      • Vitamin D
      • Magnesium
      • Breathing
      • Vision
      • Anxiety
      • Hydration
      • Sleep
        • White Noise / Background Noise
        • Napping
        • Wakefulness
      • Nutrition
        • Intermittent Fasting
        • Alcohol
      • Exercise
        • When to exercise
      • Standing
      • Longevity
    • Learning
    • Laundry
    • Hygiene
      • Oral Hygiene
        • Buying a Toothbrush
        • Flossing
    • Raynaud’s
    • Skin Care
      • Face Care
      • Sun Protection
    • Hair
      • Pomade
    • Sore Feet
  • Ideas
    • Leveraging and Training our Brain's autopilot
    • Keys
    • Mental Models
  • Craft
    • Coffee
    • Parenting
    • Handywork
    • Cooking
    • Golf
      • Personal
  • Travel
    • Flying
    • Does wearing a hat keep you cooler in the heat?
  • Cryptocurrency
  • English
  • Miscellaneous
  • GTD
  • Quantum Computing
  • German
  • German Culture
  • Mechanical Keyboards
  • Mosquitoes
Powered by GitBook
On this page
  • Notes
  • Links
  1. Software

Shell Scripting

PreviousTerminalNextmacOS

Last updated 3 years ago

Notes on shell scripting, as well as some zsh- and bash-specific stuff.

Notes

  • zsh has two representations for array variables like PATH and CDPATH, of which the lower case variant is an actual array

  • ~ is a special character. Therefore, if you use it in a string variable and then try to do something with it, ~ will be evaluated literally, and not expanded to the value of $HOME. So use $HOME in these cases instead

  • Functions can be declared with/without function keyword and parens

    • my_function (), function my_function, etc

  • To pass arguments to a function, just use spaces

    • my_function "hello" "world"

    • If passing variables, sometimes it's good practice to put them in quotes in case the variables have spaces in them

  • The export keyword is only necessary when sub-processes need access to the variable

    • So if you don't use export, when you execute programs they won't have access to the variable. But the resulting shell will

  • To do string transformation on strings in zsh upon expansion, use

  • To rename the current directory: mv ../current_dir ../new_dir_name or mv ../{test,test2} (uses brace expansion for brevity)

  • (tl;dr: there's not a great way to do this). This would be nice for code readability

  • Can't assign variable to command exit status directly

    • But can get around it using $?:

      • (grep foo bar.txt); var1=$?

    • Can get around this if you need to by using this guy instead: ꞉ -- it's a different character but looks pretty similar

  • For conditionals, 0 is truthy and 1 is falsey

  • For multline string usage, use

  • For stripping trailing characters, use

Links

expansion flags
Assigning test/[[]] results to a variable
bash - Assigning exit code to a shell local variable - Unix & Linux Stack Exchange
macOS has a weird quirk where filenames with a : show a / for them instead
HereDoc
parameter expansion %: ${var%/}, (stripping /)
Lowercasing and Uppercasing strings in bash/zsh
Bash scripting cheatsheet
Assigning default values to shell variables with a single command in bash - Stack Overflow
zsh - string substitutions: all occurrences - Unix & Linux Stack Exchange
A Quick Primer on Bash Booleans (StackOverflow)
bash - How to change the output color of echo in Linux - Stack Overflow
How to exit bash function if error? - Stack Overflow
Shell Tools and Scripting · the missing semester of your cs education