Shell Tricks: quick start scripts


Here’s another little Bash function. It makes it possible to create a new script in one command, creating the file with shebang, making it executable, and opening it in your editor.

Just run newscript scriptname.rb to create a new ruby script with a /usr/bin/env ruby shebang. It also recognizes Python, Perl, and bash extensions, add more as needed.

You can also create a directory with “skeleton” scripts. If you have a list of includes that you always use in your shell scripts, add a ext.txt file to that directory (e.g. rb.txt or py.txt). By default that directory is ~/.newscript_defaults, but you can modify it in the config section. As an example, you might have a sh.txt file that sources a file of common functions you use in bash scripts, or an rb.txt file that includes contains # encoding: utf-8 to be appended after the shebang.

Just add this to ~/.bash_profile, or wherever you source your shell functions from. You’ll need to edit your script location in the scriptdir variable at the top.

# Touch, make executable and start editing a new script
# $ newscript
# edit default shebangs within the function
# include additional skeleton files as [extension].txt
# in the $defaults_txt folder defined in config
newscript() {
	# Config
	# where your scripts are stored
	local scriptdir=~/scripts/
	# if no extension is provided, default to
	local default_ext=rb
	# optional, where skeleton scripts (e.g. rb.txt) are stored
	local defaults_txt=~/.newscript_defaults/
	# End config
	local filename="${scriptdir%/}/$1"

	if [[ $# == 0 ]]; then # no argument, display help and exit
		echo -e "newscript: touch, make executable and \
start editing a new script.\n\033[31;1mError:\033[37;1m Missing filename\033[0m\n\n\
Usage: mynewscript SCRIPT_NAME.ext\n"
		return 1
	# get the extension from the filename
	# if there's no extenstion, add default
	if [[ $ext == $filename ]]; then
	# if no script with this name already exists
	if [ ! -f $filename ]; then

		# create a file for the given extension with appropriate shebang
		case $ext in
			rb ) echo -e "#!/usr/bin/env ruby" >> $filename;;
			py ) echo -e "#!/usr/bin/env python" >> $filename;;
			pl ) echo -e "#!/usr/bin/env perl" >> $filename;;
			sh | bash ) echo -e "#!/bin/bash" >> $filename;;
			zsh | bash ) echo -e "#!/bin/zbash" >> $filename;;
			 * ) touch $filename;; # any other extension create blank file
		# if skeleton files directory and a txt for the given extension exist
		if [[ -d ${defaults_txt%/} && -f ${defaults_txt%/}/$ext.txt ]]; then
			# concatenate it to the file
			cat ${defaults_txt%/}/$ext.txt >> $filename
		# Add trailing newline to the new script
		echo -ne "\n" >> $filename
		# Make it executable
		chmod a+x "$filename"
		echo -e "\033[32;1m$filename\033[0m"
	else # Specified filename already exists
		echo -e "\033[31;1mFile exists: $filename\033[0m"
	# Edit the script
	$EDITOR "$filename"


Ryan Irelan has produced a series of shell trick videos based on posts. Readers can get 10% off using the coupon code TERPSTRA.