Learn to Code via Tutorials on Repl.it!

← Back to all posts
Bash Scripting
SigmaPhi

Important Notice

  • This tutorial is a work in progress, expect more topics to be covered later
  • Everything in this tutorial works in Repl.it, unless stated otherwise
  • This is important to mention because it can overcome a suprising number of Repl.it's limitiations
  • This tutorial assumes that you have basic knowledge of programming, specifically variables and functions (also known as procedures, methods)

What is Bash

Bash (Bourne Again SHell) is a open-source shell for the GNU project (replacing the Bourne Shell). It is the default shell for most Linux distros, macOS aswell, and in Windows 10 via WSL.

The bash language is a shell scripting language, used for configuration, package management, automation of routine tasks and more. It is similar in purpose to powershell and (somewhat less so with) applescript.
Everything covered in this tutorial can be utilized in the Repl.it implemenation of Bash.

The (non comprehensive) list of known limitations (as of April 2019):

  • unavailabilty sudo (for security reasons)
  • aplay command (audio programming is not possible)

Basics of Bash

Hello World

The function echo and printf can be used to print to the terminal

I recommend printf if you need to format tabs or newline as it is more consistent across different terminals


Everything is a string

In bash everything is a string and what they do is dependent on context.
Bash uses spaces as delimiters (seperators) so, hello world is converted to the strings 'hello' and 'world'. Use single or double quotes to make it a single string, ie: "hello world" or 'hello world'.
The first string in a line is used as the command

Note:

echo hello, echo "hello", "echo" "hello" are equivalent, but not "echo hello" as the former are two strings and the latter is one string


Variables

It is important to be careful with spaces during assignment

The $ in $Hello tells Bash to replace the string Hello with the variable Hello.

Reassignment & Constants


Command Flags

Flags are used to toggle specific options
Single hypens '-' are used for multiple single letter flags

Double hypens '-' are used for one multi-letter flag


Functions

Special Parameters

Function Declaration

Functions are declared as follows


Bash as a Control Language

Bash is a fundamental domain specific language in linux, and serves as it's control interface, like Apple's applescript and Window's powershell
It can be used to automate many routine tasks such as file management

Linux File Commands

The basic file system of Linux is structured as follows (Only relevant files are shown)
The starting directory is called the root directory '/', which is approximately equivalent to C:\ on windows

File Command are

Compiling with GCC

@Scoder12 had provided the general idea of using Bash to compile programs
GCC, Make and Git and installed in Repl.it

Makefiles

Makefiles can be used to reduce compilation time by only compiling the changed files

Linking Libraries

Default Linux libraries (ncurses.h, unistd.h) are already included in Repl.it
Any unavailable libraries (SDL2, SFML, ...) can be downloaded from their respective sites and then uploaded to Repl.it to be used in your project or cloned and compiled via git

Assembly

GCC can compile C or C++ to assembly


Git

It can also be used to access git

Here is a good post on using git by @eankeen

Voters
MATHyou
NelsonDiaz4
G0RG3
JasonSidell
fy4l
ThitherSpoon955
programmeruser
Giotto88
Laurentgth
Highwayman
Comments
hotnewtop
alexvillarreal

I assume we can’t have a way to do the equivalent of a “sudo apt-install g++”? In order to work with something like an assembly file with NASM/YASM.

SigmaPhi

g++ is installed by default, so you can compile assembly but you need to use AT&T syntax instead of NASM though.

Scoder12

@alexvillarreal If @SigmaPhi is correct, it is installed but if you have and dependencies you need to install use a polygott repl which is beta language that allows you to install things using a command called install-pkg which is basically sudo apt-get install. It is a makefile, not a bash script though.
@SigmaPhi maybe you should put something like this in the tutorial

I am a repl.it helper. If this is a good tip, please upvote.

SigmaPhi

@Scoder12 Cool, I didn't know about the install-pkg command. I had seen your proof of concept of ncurses on C, and was wondering what the polygott repl was. I didn't see them listed in the languages (It seems that it is unlisted, are there any other beta languages btw ?)

Makefiles are pretty relevant, so I will be adding more info. Also does Repl.it have a WYSIWYG markdown editor ? (That would be a cool feature!).

Scoder12

@SigmaPhi not yet put you can post it on https://repl.it/feedback

CrazyVideoGamer

@SigmaPhi nah, it's just a bash repl. There is a bug post, and it had an answer saying that the bash repl is the same as the polygott repl. All you have to do is add a Makefile. You can still use install-pkg. If you really want to use a polygott repl, even though you don't need to, just use https://repl.it/new/polygott_beta . So no, it is not removed.

CrazyVideoGamer

@SigmaPhi Now it does, just click the preview button when viewing markdown files. A markdown pannel should appear (to the right of the console).

SigmaPhi

This Tutorial is a work in progress

citlayik

is it possible to use expect?
#!/usr/bin/expect doesn't work and neither does the expect command exist.