Skip to content
Sign upLog in
← Back to Community
Bash Scripting
Profile icon
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

> echo "hello world!" #echo automatically appends a newline hello world #'-n' flag is used to remove the newline (echo -n "str") > printf "hello world!\n" hello world

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

> echo hello world #The strings are 'echo', 'hello', 'world' #The first string echo is the command #'hello' and 'world' are the first and second arguments

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

> Name=Amjad #assign "Amjad" to Name > Name =Amjad #call Name with argument "=Amjad" > Name = Amjad #call Name with arguments '=' and "Amjad"

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

> Name=Amjad #Mutable (non-constant) variables > echo $Name #The '$' is used Amjad

Reassignment & Constants

> Name="Haya" #Reassignment > echo "$Name" #"$NAME" is equal to $NAME Haya > echo '"$NAME"' #'"$NAME"' is not equal to $NAME $NAME readonly Change="Constant" #Constant variables > Change="Changing" #Causes error as variable is constant

Command Flags

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

> rm -rf "dir" rm #remove command -r #option: remove folders recursively f #option: remove files "dir" #directory to delete

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

> git --help

Functions

Special Parameters

$1 #First parameter $2 #Second parameter $n #Nth parameter $# #Number of parameters $? #Return value of most recent function $_
#$* and [email protected] are identical if not quoted #Otherwise they are different "$*" = "$1 $2 $3 $4 ..." "[email protected]" = ""$1""$2""$3"..."

Function Declaration

Functions are declared as follows

fn_name(){ #arguments are not placed here #function body } fn_name args #function call

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

/bin /home /user /Desktop ... /usr /etc ...

File Command are

cd #change working (current) directory ls #list segments (folders) mkdir #make (new) directory (folder) rm #remove file or folder

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

#Makefile .PHONY: build_c gcc -o output source.c ./output .PHONY: build_cpp g++ -o output source.cpp ./output
> make build_c > make build_cpp

Linking Libraries

> g++ -o main main.cpp -lNAME_OF_LIBRARY > g++ -o main main.cpp -lncurses #to link ncurses

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

//source.c //C source int main(){ return 0; }
> gcc -S -o assembly.s source.c #Compile to assembly
; assembly.s ; Assembly output .file "main.c" .text .globl main .type main, @function main: .LFB0: .cfi_startproc movl $0, %eax ret .cfi_endproc .LFE0: .size main, .-main .ident "GCC: (GNU) 8.1.0" .section .note.GNU-stack,"",@progbits
> gcc -c -o program.o assembly.s #Assemble > gcc -o executable program.o #Linking > ./executable #Execution

Git

It can also be used to access git

> git --help > git clone "git-repo-url"

Here is a good post on using git by @eankeen

Voters
Profile icon
MATHyou
Profile icon
ReUnyMaster
Profile icon
G0RG3
Profile icon
JasonSidell
Profile icon
fy4l
Profile icon
ThitherSpoon955
Profile icon
programmeruser
Profile icon
Giotto88
Profile icon
Laurentgth
Profile icon
Highwayman
Comments
hotnewtop
Profile icon
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.

Profile icon
SigmaPhi

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

Profile icon
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.

Profile icon
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!).

Profile icon
Scoder12

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

Profile icon
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.

Profile icon
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).

Profile icon
SigmaPhi

This Tutorial is a work in progress

Profile icon
hamzamaqsood3

Yeah, it does. I was how to get fiberglass out of skin surprised too. Even more surprised when I found out it can do links too. Like a markdown link with custom text. IT DOES THAT.

Profile icon
citlayik

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