Skip to content

Instantly share code, notes, and snippets.

@helpermethod
Last active September 25, 2019 16:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save helpermethod/ce1be4205960f074f6f5f2a77669f31f to your computer and use it in GitHub Desktop.
Save helpermethod/ce1be4205960f074f6f5f2a77669f31f to your computer and use it in GitHub Desktop.
A bash function for prepending text to a file

Motivation

While bash lets you easily append to a file, there's no simple way to prepend to a file. The prepend function provides that functionality without relying on external tools like sed.

prepend() {
  printf '%s%s' "$1" "$(< $2)" > "$2"
}

Setup

Add the function to your .bashrc file.

Usage

$ prepend 'Text you want to prepend' path/to/file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment