Image for post
Image for post

Getting started with Bash scripting

As a software developer, knowing how to navigate the command line or cli, can be a powerful skill although at first a bit intimidating. With that in mind, I’ll share some of my discoveries which I find useful when using cli.

First is a good to make the distinction that bash, zsh, fish and other shells can be understood as the language utilized within the command line to run programs and commands. Some these basic commands include cd to change directory, pwd to print the working directory, and so on. But the command line becomes more powerful when you write and execute scripts which are a list of commands written within a shell file.

Here are some ways I’ve implemented some beginner level scripting.

First I created a shell file which could be a .sh (shell), .bash, .zsh or whichever shell your command line is running.

so:

touch demo.sh

Next, you’ll need to know what shell your command line is using. You can find that out by running the command:

echo $SHELL

echo outputs the value of the system or global variable SHELL

So now, within the shell file you establish the shell that’ll run your script by providing the absolute path to its location in memory. This line is called shebang line and is written like so:

#!/bin/zsh

With the shebang line established, you can write a simple script like echoing “Hello” to your screen. And you would run the file by executing ./script_filename or in this case ./demo.sh

#!/bin/zsh
echo "Hello"

Then you could do more interesting things using functions, which are available to shell languages like any other programming language, and the shell profile file which is a file that includes special configurations for the shell. To find this file, which is usually located within the home directory, run the command:

ls -a

This will provide a list of all the files within your current directory, including hidden files which are represented with a dot or period before their name. This file is easy to identify because, no matter the shell, it will include “profile” in its name. In this case it would be:

.zprofile

So now within this file you can create custom commands using functions. To do so, you define the function, by naming it and instructing some logic or command to run and then call the function.

function desktop {
cd /Users/$USER/Desktop/$@
}

This function called “desktop” takes you to your Desktop directory from anywhere within your system because its running change directory, cd, using absolute path, which starts from the root “/” directory. Even more, the /”$@” within the function allows you access to directories that are within the Desktop directory meaning that, within your command line, you can simply run:

desktop some_directory

and it’ll change you into that directory.

You can also set aliases for commands that are heavily used like the git commands. So for example:

alias gc="git clone" 
alias gcam="git commit -am"

And you can also do other random things like open up applications from within your command line like the Apple Music app:

function music {
open /System/Applications/Music.app
}

After you’ve added some custom scripts, you can either “source” the .zprofile file or custom script file, which means to execute or make the file changes take effect within the current shell, or restart your terminal.

And so these are just some simple ideas that you can implement using scripting in bash or any shell you may be running in your command line. The takeaway is that the command line can be very useful once you know how to navigate it and begin writing some complex scripts.

Flatiron School alumni and Full Stack web developer.

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