Bash Scripts with Arguments

Make your Mac great again. With full example code!

Image for post
Image for post
Photo by Stanislav Kondratiev on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

Sooner or later you’re going to find a command line utility that help you write your code. An example of this is SwiftLint, a tool that I used as a command line tool due to the security restrictions at a bank.

This guide helps you to write a bash script — with arguments — that you can use in your terminal to create an animated GIF from a movie file.


  • Some knowledge of the Mac terminal (guide HERE)
  • This tutorial is going to use VIM (guide HERE), but you could use any text editor


Bash: “Bourne again shell”. There are many shells that can run UNIX commands, and the one used on the Mac Terminal is Bash

Command line: An interface for interacting with the operating system

Shebang: A String at the beginning of a script to indicate execution

Shell: A user interface for accessing an operating system’s services

Terminal: A command line interface to control the UNIX-based operating system that underlies the Mac

VIM: This is a contraction of Vi and IMproved. That is, it is a clone (with additions) of the vi text editor for Unix. In other words, it is a command-line interface for creating and manipulating files


I love ffmpeg, and it is a great way to create animated gif files for use, well on Medium (this is my use).

Remembering the parameters for the command line tool is a challenge in itself, and the input file name and output file name change depending on which file that you are dealing with.

Chaos can ensue. Can’t we just make this process easier?

Pro-tip: This is how to make using a command line tool easier.

A little help…

Where each of the following commands are prefixed by, $, which indicates that this should be typed into the terminal. To make this clear I’ve written it as a quote followed by the $ symbol so you will see the following (followed by a command to type) within this guide:


Prework: Installing ffmpeg

To do so you will need to install homebrew through

$ /usr/bin/ruby -e “$(curl -fsSL

and then install ffmpeg

$ brew install ffmpeg

Implementing your bash script

Creating the bash script

Using VIM we create our bash script using the following command from the command line.

$ vim ffmpegscript

Add the shebang line

From within vim we need to tell the command line to run the code using bash.

#!/usr/bin/env bash

Add the script

FFMPEG has a number of parameters that we can pass to it. The reason for this script is to have less of them, and to make it a bit easier to use FFMPEG.

The first parameter- INPUT is the file name of the movie file.
The second parameter- OUPUT is the file name of the output GIF
The third parameter- FRAMESIZE is the output frame of the GIF. If one is not provided, the default value 1200x720 is provided.

With these parameters, the ffmpeg code is run.

INPUT=${1?Error: no input filename given}
OUTPUT=${2?Error: no output.gif filename given}

ffmpeg -i $INPUT -s $FRAMESIZE -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=10 > $OUTPUT
echo "FFMPEG script has been run"

Update the script to have a help function (optional)

In the script, there is a function that will be called if we request help, and this simply echo (that is, prints) values to the console

There is then a small function that is called when we use ffmpegscript -h to display the help.

What happens here is using the following case statement which is called by the getops command which reads through the options provided to the command and switches to the correct option. The ;; relates to an option being complete at the end of each case.

This will then print out the following to the console:

Syntax: ffmpegscript output.gif FrameSizeWxH

Save the script

From within VIM we can (if we were editing a quick nudge of the escape key will allow us to do this) use the save command :wq

$ :wq

Make the script executable

From terminal run the following to make the script executable

$ chmod +x ffmpegscript

Run the script

From the terminal we run the script. The ./ part of this command indicates that the file is in the same folder. Now when you type a command the shell looks up it’s list of directories as specified by the PATH variable (and we shall place it there in the next part of this guide).

$ ./ffmpegscript

Make the script Globally available

Make the script available globally

  1. Copy the script to /usr/local/bin by running the following command (sudo meaning you are running this as the super user):

$ sudo cp ./ffmpegscript /usr/local/bin

you may well be asked for you password. This is no problem — don’t panic as this is a side-effect of using sudo.

2. Set the environment variable

  • open the bash_profile

$ vim .bash_profile

  • Add the path export PATH=~bin:$PATH

Here is my .bash_profile:

Image for post
Image for post

3. save the result using :wq


4. Profit

You can now run the file from anywhere. In the case of this script we can type ffmpeg

$ ffmpeg

Making further edits

Where further edits are made (from the original folder) these changes need to be copied back to the /usr/local/bin folder so the new version can be run. The way to do this? cp once again!

$ sudo cp ./ffmpegscript /usr/local/bin

Immediately the new version of the script will be run.

Of course, the machine may well ask for your password again. This should be no problem for you, you look like you know your password (well done).

The full script:


Writing Bash scripts like this can save a great deal of time. This example script uses ffmeg to create an animated gif from a movie. We can access this script from anywhere; well done!

The Twitter contact:

Any questions? You can get in touch with me HERE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store