Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

An Alias to Create Shell Scripts

blog/cards/an-alias-to-create-shell-scripts.jpg

Most shell scripts I create start the same, this saves a bit of time from having to create, chmod the file and set a few options.

Quick Jump: Demo Video

I recently added a new mkscript alias to my dotfiles.

You can run mkscript demo or mkscript some/deeply/nested/path/myscript and it’ll create a new script for you as long as the file doesn’t exist already. It also makes the script executeable and sets a few set -o options.

It’s a fairly small alias but size doesn’t matter. A core principle here is that you can build tools and mold your dev environment to your liking. You are doing these things because it makes you happy.

In the video below we’ll go over how it was created, which covers shell topics such as:

  • Ensuring a variable is defined
  • Ensuring a file doesn’t already exist
  • Using command groups
  • Using dirname to get the path of a file
  • Outputting a multi-line string to a new file

Demo Video

Timestamps

  • 0:25 – Developer happiness is important
  • 1:18 – Life without this alias
  • 3:28 – Using the mkscript function
  • 4:55 – Looking at the function
  • 5:20 – Using human readable variables
  • 5:22 – Making sure the file doesn’t already exist
  • 6:03 – Using return 1 instead of exit 1
  • 6:44 – 1 line if statements and command groups
  • 8:32 – Avoid clobbering existing files
  • 9:10 – Creating parent directories with mkdir -p and dirname
  • 10:31 – Redirecting a multi-line string to a file
  • 11:52 – Making the file executable
  • 12:08 – What would you put into the generated file?

References

What’s your base line shell script look like? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per month (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments