Black Pine

Bash functions

How to create and use Bash functions

  1. Include a function script

    Add an external script file in .zshrc:

    source ~/toolbox/scripts/functions.sh
  2. Write a function

    Create a functions.sh file in the desired location:

    #!/bin/bash
    # Prints the ignored files tracked by Git
    
    function -list-ignored-git() {
      git ls-files -v | grep '^h'
    }
    
    function -ignore-git() {
      git update-index --assume-unchanged "$1"
    }
    
    function -unignore-git() {
      git update-index --no-assume-unchanged "$1"
    }
  3. Source your shell config:

    source ~/.zshrc
  4. Use the functions:

    $ -ignore-git README.md
    $ -list-ignored-git
    h README.md
    $ -unignore-git README.md
    $ -list-ignored-git