🔺
2694 Words

Bash Terminal for Newbie Game Developers

The command line can seem intimidating…

But if you think of it as ‘The Ancient File Manager’. Then I think learning it becomes simpler.

If your serious about making games then Sooner or later you’re going to want to use a shell terminal. Knowing how to use the command line opens up a lot of powerful tools.

For now you can think of bash, shell, the terminal, and command line as the same thing. The differences won’t be meaningful to you yet.

In an upcoming tutorial (that I am sure to finish at some point…) I will be teaching you how to use git. This article is a prerequisite to that one.

Setup

Using Bash on Windows

If you want to follow along on windows, You have two options. (aside from running a virtual machine with Docker or Virtual Box)

1. Windows Subsystem for Linux

Or you can install WSL. WSL is less limited. It is developed and supported by Microsoft. and the install instructions are pretty simple. When I need to used windows for development I would go with WSL.

wsl --install

2. Git Bash for Windows (limited)

You can install git for windows, which includes a limited bash emulator. Be aware that it has some differences and limits. You can use ls /bin in git bash to get a list of available commands. If you want to check for a specific command you can use ls /bin | grep '<search>'

Setup on Linux and Mac

For linux and mac users, Your computer should come with a terminal application to access your command line.

For this tutorial you don’t need to but I always enable the run command as login shell setting.

Some programs like rvm(Ruby Version Manager) will ask you to want you to enable it.

Ubuntu: preferences > profiles > command > run command as login shell.

I think it’s enabled by default on mac. Mac: preferences > profiles > > run command as login shell.

run as login shell setting

Your Fist Date with a command line

when you type a command and hit enter. Your computer runs that command.

1
2
$ echo "hello world!"
hello world!

hello world

You can cycle through your command history with the arrow keys. When your typing, you can hit tab, it will try to auto complete what your typing. especially file paths.

You can search your command history with ctrl+r You can cancel with ctrl+c. You can exit, close or stop things with ctrl+d, including your terminal.

On mac just replace ctrl with command

when your screen gets to cluttered, you can type clear to reset the display.

Let Pretend the Terminal is a File Manager.

Everything you can do with a file manager you can to in a terminal.

Its not just a file manager, It can do far more. But that analogy will help you understand it.

ls cd pwd

If I type ls. and hit enter then, bash will print out a list of files and folders in my current directory.

1
2
3
4
$ ls
Applications  code       Downloads              Music      Public     Videos
Branding      Desktop    inkSwan                Pictures   scripts
carla         Documents  MinecraftModsAndStuff  Portfolio  Templates

Instead of double clicking on folders to open them, You type cd, as in change directory, followed by a file path.

1
2
3
$ cd code
$ pwd
/home/sean/code/games

pwd stands for ‘print working directory’.

You can go back folder with cd ..

You can also Change directories into the current directory with cd . You could also can get the same effect by doing nothing.

In bash file paths are separated with forward slashes. cd accepts a file path so we can jump past multiple folder at a time. cd code/games/example or cd ../../gameJams this is a relative path. it starts from your current directory.

We also have absolute file paths. those start with a leading slash. and begin from the root folder in your computer. when we type pwd it prints a absolute path.

1
2
3
4
5
6
$ cd / 
$ ls
bin   etc   lib32   lost+found  opt       root  srv  usr
boot  home  lib64   media       proc      run   sys  var
dev   lib   libx32  mnt         recovery  sbin  tmp
$ cd ~/

A leading ~/ will start from your users home folder. I use that a lot.

ls also accepts a file path. That allows you to look around without changing directories.

The Terminal has a User Manual.

man tldr

help flag and man command

If your like me, then you will will forget the details about command and you will desire to look them up.

There is a manual command. man less

1
2
3
4
5
6
7
NAME
       less - opposite of more
...
DESCRIPTION
       Less is a program similar to more, but it  has  many  more  features.
       Less does not have to read the entire...
...

I love how this description assumes you know what more does

You can scroll up or down the page with arrow keys, or if your a nerd you can use j and k to scroll.

If you’re really a nerd you can search the manual with a slash /help. Then you an view the next instance of the search with n and the previous with shift+n

here it is saying we can view a help/ summery page if we pass the --help flag to less.

1
2
3
-? or --help
    This  option  displays  a summary of the commands accepted by less (the
    same as the h command)...

So you can exit the manual the with the q or esc keys.

and if we type less --help then we get a page kinda like the manual but less technical and hopefully with more examples. Most commands have their own help page and accept the same help flag.

“But The Manual is too Long!!!”

Your in luck! Kind people of the internet have created the tldr pages.

The manual is very detailed, and often lacks examples. Thats why I normally check tldr first.

1
2
3
4
5
6
$ tldr less
Open a file for interactive reading, allowing scrolling and search.
More information: https://greenwoodsoftware.com/less/.

- Open a file:
    less source_file

sadly the tldr package is not installed by default. For now you can use the tldr live demo. I will talk about installing software further along.

If all else fails, Google is a freind of command details.

Create/ View/ Edit, Files and Folders

touch mkdir less cat nano vim

In the terminal you can create new empty folders with the make directory command. You can also create empty files with the touch command

1
2
3
4
$ mkdir tmp_dir
$ touch tmp_dir/a_text_file.txt
$ ls tmp_dir
a_text_file.txt

The console often has a built in text editor, mines called nano. Im not a fan of nano, but it does come installed by default and a newbie will fair better with nano than vim at first.

Nano Looks like this:

Nano

And this is what vim looks like:

Vim

Instead of using touch I tend to open a new file with vim and then save it afterwards. I believe you can do the same with nano.

1
2
$ vim tmp_dir/new_file_with_vim.txt
$ nano tmp_dir/new_file_with_nano.txt

Sometimes you don’t need to edit a file you just need to view it. I’ll use cat for that, tldr cat cat concatenates files, but 90% of the time its just used to display a file.

Fun thing, if you call cat without an argument and redirect the output into a file, You can create a file with contents right from the terminal. You can also redirect the output from other commands into files. So i could pipe ls into a file.

For long files you can also use less, You can navigate it exactly like the manual.

Picking a Code Editor

If your going to do any programming outside of your game engine you will want a code editor. The text editor you use is more or less a personal choice.

My dads is an old school developer and he exclusively use gvim (graphical vim) Most developers these days use a text editor like vscode, or atom.

I used to mainly use vscode and occasionally vim for stuff like config files. When I’ll spend a lot of time logged in to remote servers, Vim is really handy.

My brother has godot configured to use neo-vim instead of godots code editor.

Recently I ditched vscode and have been using solely neo-vim as my editor. However it requires so much configuration that I cant recommend it.

Basically I am saying start with vscode or atom.

Copy & Paste + Cut + Move + Rename & Delete

cp mv mkdir rm

You’re familiar with moving around files!

You can do the same thing in the command line with the mv(move) command.

You can also use mv to rename files.

1
2
3
4
5
6
7
$ mkdir another_dir
$ mv tmp_dir/a_text_file.txt another_dir/
$ ls tmp_dir
$ mv tmp_dir tmp
$ cd tmp
$ ls
just_markdown.md

Some commands will accept paths with wild cards in them. TODO: correct the example files and paths.

1
2
3
4
5
6
$ touch tmp_dir/some_markdown.md
$ mv tmp_dir/*.txt another_dir/
$ ls tmp_dir/
some_markdown.md
$ ls another_dir/
.....TODO: add stuff here.

you can copy files with cp.

1
2
$ cp just_markdown.md example.md
$ ls 

You can delete files the remove command.

1
2
$ mv example.md ~/trash
$ rm ~/trash/example.md

Sometimes commands like rm it will complain about directories. In these cases you can typically pass the recursive flag. That tells it to delete the folder and all of its contents.

1
2
3
4
$ mkdir remove_me
$ rm remove_me
rm: cannot remove 'remove_me': Is a directory
$ rm -r remove_me

Plz do be careful with the rm command.

also: I can’t be held liable if you accidentally delete the entire contents of your computer after reading this tutorial.

File Search with Find

find fd

File managers normally have a file search feature. the command line has find.

1
2
3
4
$ tldr find
...
find root_path -name '*.ext'
$ find . -maxdepth 3 -name '*.txt'

find is powerful, but to most of the time it is to cumbersome for me to actually use.

While researching this tutorial I discover the fd-find package. I have already used fd far more often then I ever used find.

If you want to use it you will need to install it.

Search Text with Grep

grep

You can search for text inside files with grep.

1
$ grep 'something' example_file.txt

You can also grep recursively through every file in a project. Latey I have been doing that a lot at work.

1
$ grep -r 'something' ./*

Sometimes a command produces way to much output to read. You can use grep to filter command output. For example You can pipe the output from ls into grep.

1
2
3
4
5
6
$ ls /bin | grep "less"
...
function_grep.pl
grep
grep-aptavail
...

You can do more advance searches with regex but im not going to get into that rn.

Downloading files from the interwebs.

curl wget

Sometimes you want to downloading files directly from the interwebs.

1
$ wget -P /tmp https://launcher.mojang.com/download/Minecraft.deb

You probably have either wget or curl installed by default.

A lot of software offers a install script that you can download and run with curl or wget.

Homebrew is a good example of software you install via a curl script.
You can copy to their command to your clipboard and run it in the terminal.

Your basically downloading and running a executable file fresh from the curbs of the internet.

It makes me somewhat uncomfortable. Make sure you trust the people hosting the script.

You can check whats in the executable by copying the script url and viewing it in your browser. or you can download and read the script before running it.

1
2
3
4
5
$ curl https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh > brew_install.sh
$ vim brew_install.sh
or
$ wget --output-document brew_install.sh https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh
$ nano brew_install.sh

Its not practical to check each install script. So just make sure the providers of the script are trustworthy.

And this is why we have package managers.

Compressing and decompressing

tar zip unzip gzip gunzip

In the file manager you can zip or unzip things. In the terminal you use zip, unzip, tar.

You can figure those out with tldr when you need them.

Package Managers & Installing software form the command line

sudo apt brew yum snap npm yarn pip gem

You can think of package managers as app stores.

A lot of operating systems have their own package manager. Kinda like how android and apple have different app stores.

Additionally programing languages will often have their own package managers. For example javascript has npm and yarn, Ruby has gem, python has pip, go has go, and so on.

You get it! There are Tons of package managers!

While each package manager diverges somewhat most are very similar. I will be demonstrating with apt (the default package manager for Ubuntu). Most package managers will have almost the same functions.

Typically you will see: install/add, remove/uninstall, list, update, upgrade, find/search.

You’re smart! You can consult tldr or the manual or google to learn the specifics about your package manager.

Windows

In git bash for windows you cant actually install software. so your just kinda stuck with what you got.

With WSL you can install stuff. By default it runs an ubuntu ‘subsystem’. So you’d use apt. The same package manager as ubuntu.

Windows itself has a *newer package manager called winget.

Homebrew on Mac

On mac you’ll use homebrew as your terminal package manager.

Sadly its not installed by default. As of this tutorial it installs via a curl script.

Homebrew is also available on linux if you would like to use it. Tho I don’t really see a reason why.

Linux

There are a handful of default packages managers for the different flavors of linux.

On debian based distros like ubuntu you’ll use apt.

Some distros like Fadora use yum. You probably know what yours is.

Lets install tldr

You have lived long enough without tldr.

The original client for tldr is installed with npm, Now there is a list of tldr available clients to choose from.

I like to use apt search to check if my computer already has a source for it.
Looks like my pc has two tldr clients repositories easily available, a Haskell and a Python client. Ill go ahead and install the haskell client.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ apt search tldr 

tldr/focal 0.4.0.1-1build2 amd64
  Haskell tldr client

tldr-py/focal,focal 0.7.0-3 all
  Python client for tldr: simplified and community-driven man pages

$ sudo apt install tldr
$ tldr --version
0.6.4
$ tldr --update
...
$ tldr tldr
...
- Show the tldr page for a command (hint: this is how you got here!):
  tldr 
...

On Ubuntu, you need root or superuser privileges To install packages globally. sudo simply runs the following command as a supper user.

And Some tldr clients will need you to run tldr --update right after installing it.

You can check for updates with sudo apt update and you can upgrade to those packages with sudo apt upgrade

Again, most package managers are very similar. So the details and packages might be different but the concepts are the same.

And for installing fd-find? Yeah Im done! You can figure that out yourself!

Package Repositories & sources

package repositories are like the libraries of software, code, and application you can pull from.

Most of the time the defaults are enough. When you need to add one the package your installing will tell you.

Installing the brave browser for example, it will ask you to add their repository before you can install it. (On ubuntu)

Much Wow!

I cant believe you read this whole thing!

Actually I can’t believe I even wrote this whole thing.

Yeah that was too much work.