Boost Your Productivity: Command Line and Bash Scripting with a Real-World Example

Muhammad Sabir Raza Attari
Django Unleashed
Published in
10 min readJun 16, 2024

If you are a developer or a computer science student and you don’t know about the command line, this article is a must-read. There is also a very cool project after the basics, so if you are already familiar with the basic commands, you can skip directly to that part.

Command Line Interface (CLI)

Command line in simple words is a text-based interface where you write instruction (more commonly known as command), which are then executed by the computer to perform various tasks like managing files and directories, running programs, networking, text processing, system configuration, and more.

Advantages of CLI

It might sound sound weird but CLI has quite a few advantages over Graphical User Interface (GUI):

  1. More control over the operating system (OS).
  2. Uses fewer resources than GUI.
  3. Increases your efficiency and speed by allowing you to handle thousands of files with a single command.
  4. Allows you to easily manage remote servers via Secure Shell SSH.

There are many more advantages but this should give you a general idea.

Photo by Alex Chumak on Unsplash

Essential Commands

Before diving into the basic commands of command line, I’d like to suggest you to use the bash, it command line interpreter for Unix-like operating systems. Bash comes pre-installed on most Linux distributions, in available on Windows through Windows Subsystem for Linux (WSL) or git bash, and can be easily accessed on macOS.

There are a lot of bash commands, but I’m going to mention just a few of them. Let’s dive right into them:

  • man or — help
    man short for manual is to get the documentation about the other commands. man doesn’t work on windows even if you use git bash, so — help does the job.
# for macOS and Linux
man ls
# or for windows
ls --help
  • clear
    Removes everything from the terminal to make it look nice and clean.
clear
# or just press CTRL + L
  • ls
    Lists all the folders and files in the current directory.
# hidden files and folders not included
ls
# hidden indluded
ls -a
# more detailed listing
ls -l
  • cd
    This changes your directory.
# takes you to your home directory
cd
# assume there is a downloads folder in your current directory
cd downloads/
# takes your a directory back
cd ..
  • mkdir
    Makes a new directory.
# will create a new folder named
mkdir new-folder
  • touch
    Creates a file in the current directory.
# create a txt file named file1
touch file1.txt

# creates multiple files
touch file2.txt file3.txt
  • rm
    To remove files.
# remove a file
rm file1.txt

# removes a file but also prompt you if you really want to delete it
rm -i file2.txt

# its different for directories
# must use -r flag to remove directory
rm -r downloads

# if that directory is not empty use -f flag to force it
rm -fr downloads
  • cat
    It has a bunch of uses and is a really powerful command, I’d suggest reading the documents because I won’t be mentioning all of them.
# to see what is written in a file
cat file3.txt

# to write to a file
cat > information.txt
Hello Kitty!
# press CTRL + D to get out of writing mode

# to append a new line
cat >> information.txt
Why are you called cat.
# press CTRL + D to get out of writing mode
  • echo
    Prints the given text on the terminal.
# print on terminal
echo "Hello Kitty!"

# outputs into a file
echo "Hello Kitty!" >> information.txt

There are many more commands and concepts that are really powerful, and I would recommend learning about them. However, for this article, these are enough to give you a general idea.

Shell Scripting

Shell scripting is nothing but a series of commands which are to be executed by the shell (command line-interpreter). Think of it like writing the commands we learned above in a file with some conditions and sequence to automate some task.

Key Characteristics of Shell Scripting

  1. They are used to automate some repetitive task by execute series of commands.
  2. They support variables, loops (for, while), conditional statements (if-else), functions, and error handling mechanisms.
  3. They also provide interactivity by prompting for user inputs.

Key Concepts

  • Variables: Store data which can be used later.
name="Kitty"

echo "Hello, $name!"
  • User Input: Prompts the user for input.
echo "What is your age?"
read age

echo "You are $age years old."
  • Conditional Statements: Makes decisions based on different conditions.
    -eq: Equal to
    -ne: Not equal to
    -gt: Greater than
    -lt: Less than
    -ge: Greater than or equal to
    -le: Less than or equal to
age=25

if [ "$age" -ge 18 ]; then
echo "You are an adult."
else
echo "You are not yet an adult."
fi
  • Functions: Used to organize code into reusable blocks.
# Function definition
greet() {
echo "Hello, Kitty!"
}

# Function call
greet
  • Here Document: Here Documents is used to write multi-lines texts on the commands.
# EOF is used as a delimeter to mark the starting and ending of the code
cat << EOF
Welcome to my script.
This is a multi-line message.
EOF

Project and GitHub Setup Automation Tool

A problem that I usually go through is the setting up of a new project by writing complex commands again and again and then creating its repository and pushing the project to it initially. If you are someone who makes a lot short of side projects, you must also have gone through this problem.

In order to solve this problem, we are going to write a shell script which will automate the setup of software projects. The shell script will allow the user to choose between setting up a React project, a Django project, or a project with React as frontend and Django as backend. It would also prompt the user to optionally create a GitHub repository and make initial commit after project setup.

In no way is this script the most perfect one and it obviously is not covering a wide range of frameworks, I just mentioned the two I am regularly using. Feel free to try on your own and edit and maybe improve the script. Lets get started.

  • Create .sh File

The first step is to create a file with .sh extension. I’d suggest that you use touch command for this and put the file wherever you want. Name it anything you want, I named mine “project-setup.sh”. Then simply just open the file in any text editor.

  • Shebang

It is typically the first line of the shell script. Its purpose is to specify which interpreter should be used for the execution.

#!/bin/bash
  • Project Directory Setup

Prompt the user to enter project name, create a directory with that name, and navigate to it.

echo "Enter the project name:"
read PROJECT_NAME

mkdir "$PROJECT_NAME"
cd "$PROJECT_NAME"
  • GitHub Repository Creation Function

For this part, you’d require your GitHub personal access token. You can get this token from your GitHub account by following these steps:

  1. Log in to your GitHub Account.
  2. Click on your profile icon in the top right corner of GitHub.
  3. From the dropdown menu, select Settings.
  4. In the left sidebar, click on Developer settings. (you might need to scroll down a little).
  5. Click on Personal access tokens.
  6. From the dropdown, select Tokens (classic).
  7. Click on Generate new token (classic) button.
  8. Enter your password if prompted.
  9. Enter a descriptive name for your token.
  10. Select scopes your want to grant to this token. For this script, we only need `repo` scope. Which will grant us full control over creating and managing private and public repositories.
  11. Now simply click on Generate token button.
  12. Copy and save the token somewhere secure immediately, it will only be shown once. And don’t share this token with anyone, treat it like a password.

Once you have the token, we can move on with our GitHub repository creation function by asking the user if the user wants the repository to be named same as the project, otherwise prompt him to enter the name of the repository.

Now, we’ll make a post request to the GitHub API to create a new repository. We’ll use curl to make this request. curl is a tool used to transfer data to and from servers using different protocols, including HTTP, HTTPS, FTP, and more.

create_github_repo() {
# GitHub personal access token
# Not the best way to declare it in script
# I'm leaving it upto you to find a better and secure way
TOKEN="yourPersonalAccessToken"

echo "Do you want to name the GitHub repository same as the project name? (y/n):"
read GITHUB_NAME_CHOICE

if [ "$GITHUB_NAME_CHOICE" == "y" ]; then
GITHUB_REPO_NAME="$PROJECT_NAME"
else
echo "Enter the GitHub repository name:"
read GITHUB_REPO_NAME
fi

echo "Creating GitHub repository '$GITHUB_REPO_NAME'..."

# GitHub API provided the below endpoint
curl -X POST "https://api.github.com/user/repos" \
-H "Authorization: token $TOKEN" \
-d '{"name":"'"$GITHUB_REPO_NAME"'"}'

echo "GitHub repository '$GITHUB_REPO_NAME' created successfully."
}
  • GitHub Repository Initialization Function

You’d require your GitHub username for this part. Using the username and repository name, initial commit with all of the files added will be made.

init_github() {
# GitHub username
USERNAME="Your-Github-Username"

echo "Initializing GitHub repository..."
echo "# $GITHUB_REPO_NAME" >> README.md
git init
git add *
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/$USERNAME/$GITHUB_REPO_NAME.git
git push -u origin main
}
  • React Project Setup Function

The user will be prompted if he wants to use Typescript. And the project will be initialized based on his input.

setup_react() {
echo "Do you want to create a TypeScript project? (y/n):"
read TS_CHOICE

if [ "$TS_CHOICE" == "y" ]; then
npm create vite@latest . -- --template react-ts
else
npm create vite@latest . -- --template react
fi

# npm install (if you want to)
}
  • Django Project Setup Function

The user will be prompted to create a virtual environment. If user chooses to do so, user will be asked to enter the name of the virtual environment. Then the environment will be activated, and basic commands for installing Django and starting projects will be executed. Since the Django setup does not include a .gitignore file, we will add it using Document Here and redirect its contents to the .gitignore file.

setup_django() {
echo "Do you want to create a virtual environment? (y/n):"
read VENV_CHOICE

if [ "$VENV_CHOICE" == "y" ]; then
echo "Enter the virtual environment name:"
read VENV_NAME
python -m venv "$VENV_NAME"
source "$VENV_NAME/bin/activate"
fi

pip install django
django-admin startproject "$PROJECT_NAME" .

# Create .gitignore file for Django project - take care of intendation
cat <<EOF > .gitignore
$VENV_NAME/
**/migrations/
**/local_settings.py
.vscode/
.idea/
db.sqlite3
EOF

}
  • React — Django Setup Function

This function is simple. We’ll create two directories, named frontend and backend. And call setup_react in frontend and setup_django in backend.

setup_react_django() {
mkdir frontend backend
cd frontend || exit
setup_react
cd ../backend || exit
setup_django
cd ..
}
  • Main Script Execution

This is the part where main script logic is located. The user will be prompted the main setup choices, after that the user will be prompted to decide whether he wants to create a GitHub repository. Based on his setup choice, the relevant function will be executed.

echo "Select the project setup option:"
echo "1. React Project"
echo "2. Django Project"
echo "3. React and Django"
echo "4. Create GitHub repository only"
read SETUP_CHOICE

if [ "$SETUP_CHOICE" != 4 ]; then
echo "Do you want to create a GitHub repository for this project? (y/n):"
read GITHUB_INIT_CHOICE

if [ "$GITHUB_INIT_CHOICE" == "y" ]; then
create_github_repo
fi
fi

case $SETUP_CHOICE in
1)
setup_react
if [ "$GITHUB_INIT_CHOICE" == "y" ]; then
init_github
fi
;;
2)
setup_django
if [ "$GITHUB_INIT_CHOICE" == "y" ]; then
init_github
fi
;;
3)
setup_react_django
if [ "$GITHUB_INIT_CHOICE" == "y" ]; then
init_github
fi
;;
4)
create_github_repo
init_github
;;
*)
echo "Invalid choice"
exit 1
;;
esac

This is it, now all you’ve got to do is run this script in the directory where you want to create your projects.

  • Final Shell Script
#!/bin/bash

echo "Enter the project name:"
read PROJECT_NAME
mkdir "$PROJECT_NAME"
cd "$PROJECT_NAME"

create_github_repo() {
TOKEN="yourPersonalAccessToken"

echo "Do you want to name the GitHub repository same as the project name? (y/n):"
read GITHUB_NAME_CHOICE

if [ "$GITHUB_NAME_CHOICE" == "y" ]; then
GITHUB_REPO_NAME="$PROJECT_NAME"
else
echo "Enter the GitHub repository name:"
read GITHUB_REPO_NAME
fi

echo "Creating GitHub repository '$GITHUB_REPO_NAME'..."

curl -X POST "https://api.github.com/user/repos" \
-H "Authorization: token $TOKEN" \
-d '{"name":"'"$GITHUB_REPO_NAME"'"}'

echo "GitHub repository '$GITHUB_REPO_NAME' created successfully."
}

init_github() {
USERNAME="YourUsername"

echo "Initializing GitHub repository..."
echo "# $GITHUB_REPO_NAME" >> README.md
git init
git add *
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/$USERNAME/$GITHUB_REPO_NAME.git
git push -u origin main
}

setup_react() {
echo "Do you want to create a TypeScript project? (y/n):"
read TS_CHOICE

if [ "$TS_CHOICE" == "y" ]; then
npm create vite@latest . -- --template react-ts
else
npm create vite@latest . -- --template react
fi

# npm install (if you want to)
}

setup_django() {
echo "Do you want to create a virtual environment? (y/n):"
read VENV_CHOICE

if [ "$VENV_CHOICE" == "y" ]; then
echo "Enter the virtual environment name:"
read VENV_NAME
python -m venv "$VENV_NAME"
source "$VENV_NAME/bin/activate"
fi

pip install django
django-admin startproject "$PROJECT_NAME" .

# Create .gitignore file for Django project - take care of intendation
cat <<EOF > .gitignore
$VENV_NAME/
**/migrations/
**/local_settings.py
.vscode/
.idea/
db.sqlite3
EOF

}

setup_react_django() {
mkdir frontend backend
cd frontend || exit
setup_react
cd ../backend || exit
setup_django
cd ..
}

echo "Select the project setup option:"
echo "1. React Project"
echo "2. Django Project"
echo "3. React and Django"
echo "4. Create GitHub repository only"
read SETUP_CHOICE

if [ "$SETUP_CHOICE" != 4 ]; then
echo "Do you want to create a GitHub repository for this project? (y/n):"
read GITHUB_INIT_CHOICE

if [ "$GITHUB_INIT_CHOICE" == "y" ]; then
create_github_repo
fi
fi

case $SETUP_CHOICE in
1)
setup_react
if [ "$GITHUB_INIT_CHOICE" == "y" ]; then
init_github
fi
;;
2)
setup_django
if [ "$GITHUB_INIT_CHOICE" == "y" ]; then
init_github
fi
;;
3)
setup_react_django
if [ "$GITHUB_INIT_CHOICE" == "y" ]; then
init_github
fi
;;
4)
create_github_repo
init_github
;;
*)
echo "Invalid choice"
exit 1
;;
esac
  • Running the Shell Script

In order to run the shell script, you have to make it executable. Navigate to the directory where your shell script is located and run the following command to make it executable.

chmod +x setup_project.sh

Once it is executable, you can execute the script by running the following command.

./setup_project.sh

Conclusion

I hope each and every one of you got something to learn from this article. The aim was not to just talk about all of the commands of the terminal, rather it was to make something useful out of even bare minimum.

I suggest reading more about the command line and shell scripting through documentation, YouTube videos, or other articles, as there are many more commands and concepts yet to explore.

Connect with me

If you have any suggestions or questions, feel free to reach out to me on LinkedIn.

LinkedIn: https://www.linkedin.com/in/muhammad-sabir07/
Email: muhammadsabirrazaa@gmail.com
Github: https://github.com/Muhammad-Sabir

--

--