Revision as of 16:14, 14 March 2022 by Sjwhitak (talk | contribs) (Wooo syntax highlighting is awesome)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to:navigation Jump to:search

Basic Bash

It was too much effort to make a full guide so this is just the basics.

What is bash?

It is literally just a file full of commands. It allows users to write automation scripts because us sweaty nerds are the laziest human beings the human race has to offer. Typing commands over and over again is hard work and requires a mild amount of effort.

Creating a bash file

Bash files use the .sh file extension. They have two basic things they need in order to run. They need a shebang line and to be made executable.

  • The shebang line just goes at the beginning of the file and tells your computer what to do with the file. For bash files the shebang line is just "#!/bin/bash"
  • To make the file executable, just use the classic "chmod +x FILE_NAME"


Variables are super simple, here is a basic example of a hello world program bash file.


greeting="You are a warm popsicle"
echo $greeting

When creating and assigning a value to a variable, make sure to include no spaces between the variable name, the equal sign, and the value of the variable. Then when you want to call a variable for use, like the greeting, we use a dollar sign to indicate that it is a variable. Note, variables like $1, $0, $11 are reserved for command line arguments so avoid using them.


Read values inline:


echo "Guess a number" 
# Use read to create the number variable and
# immediately set the variable to the input
read number
echo "You get slapped $number times."

Read parameters from command-line:


# Prints the name of the file
echo $0
# Prints the first parameter passed to the file
echo $1

# $@ is used to represent all of the command line inputs
# Here we are just iterating through all command line inputs
for param in "$@"
     echo $param


The operators for bash are:

  • Equal: -eq
  • Not equal: -ne
  • Less than or equal: -le
  • Less than: -lt
  • Greater than or equal: -ge
  • Greater than: -gt
  • Is null: -z
  • or: ||
  • and: &&

As a side note, when comparing two strings, always put strings or variables referencing strings in parenthesis and compare using == or !=. syntaxhighlight lang="bash"> if [ "$foo" == "$bar"]

  1. or

if [ "$foo" != "$bar"] </syntaxhighlight>

If, Elif, Else


echo "Enter a number:"
read number

# if and elif both have a "then" associated with it
if [ $number -eq 1 ]
     echo 1
elif [[ $number -eq 2 ]]
     echo 2
     echo "You can't get anything right you hopeless loser"

This is a simple example that just reads in a number and checks to see if the number is either 1, 2, or, if neither of these are true, will just insult you. The syntax here is pretty simple. You have the if statement which uses square brackets for the conditional and elif statements which use double square brackets. There is also the default else statement and the whole statement is closed off by a fi (if backwards).


There are three types of loops:



# Iterates through all objects in an array, all inputsguessed, etc.
for word in $paragraph
     echo $word



# Runs while the conditional is being met
while [ $index -lt 5 ]
     echo $index
     # Incrementing values is obnoxious
     index=$((index + 1))



# Runs until the conditional is met
until [ $index -eq 5 ]
     echo $index
     # Still obnoxious
     index=$((index + 1))

Iterating through files in a directory


# Path to the directory we want to check out
# The for loop is back baby
for file in $files
     echo $file
     # Prints out the absolute path of each file

Bash is able to automatically detect whether a variable represents a directory or not. It's magic and is definitely not regex.


You can use aliases to create cli commands from bash files. Aliases can be set in your ~/.bashrc file. Make sure to make your bash file executable and move it to your

alias epiccommand='./'
alias epiccommand='./ "Nerds"'