File System
Your computer's file system organizes it's files and directories into a tree like structure. The main directory is the first one that was created and is referred to as the root directory or parent directory. Every root or parent directory can have as many child directory and files as you can imagine as long as they are nested.
Sending your computer a command is the same as asking someone to do something; in this case, your computer.
Installing Git Bash
To get the bash CLI on windows, we will need to download git bash. Git Bash is a source control management system for Windows. It allows users to type Git commands that make source code management easier through versioning and commit history.
Step 1
Download the git bash command setup from the official website git-scm.com.
Step 2
Download the installer.
Step 3
Run the .exe file you just downloaded and follow the instructions in the installer.
Step 4
Run Git Bash by right-clicking on any folder and selecting the Git Bash Here option from the context menu(right-click menu).
Setting Up Your Command Line Interface
The command line is used to create manipulate and find files and folders. There are different forms of command lines with short form CLI. To setup our interface, we will be using the bash CLI.
Bash is an open source command line and is the default for Linus and MacOS. To access Bash in MacOS, you can use an application called Terminal.
First open the Applications folder, then open the Utilities folder.
Once you’re in the Utilities folder you will see the application Terminal. Open the Terminal application and you’re ready to go!
For ease of access later, you can keep Terminal in your Dock. Simply right click (alt-click) the Terminal icon in your dock, then select “Options”, then “Keep In Dock.”
Let's look at some commands and what they do.
Command Lines
$ ls
command checks what directory you are in and looks for and lists the files within it.ls -a
is used to check the contents of the working directory in more detail.ls -t
displays files and directories in the same order they were created.ls -l
list a detailed contents of directories with files permissions in a long format which includes, file owner's username, access rights, size of file in bytes, number of hard links, name of group that owns the file, date and time the file was last modified, name of the file or directory.ls -alt
lists all contents, including hidden files and directories, in long format, ordered by the date and time they were last modified.$ pwd
outputs the directory you are currently working on. It's also called the working directory. Pwd means print working directory$ cd
means change directory and it does just that. It switches you into the directory you specify. When a file, directory or program is passed, it is called an argument.$ mkdir
means make directories and is used to create directories(folders).$ touch
is used to create a file in the working directory. It takes the file name as an argument and creates an empty file.cat
outputs contents of an individual file in the terminal.clear
is used to clear your terminal.tab
is used to autocomplete your commands.cp
command is used to copy files or directories.cp source.txt destination.txt
andcp source.txt destination/
to copy files from source directory into destination directory. Also used to copy multiple files into a directory with a list of source files as the first arguments, and the destination directory as the last argument*
are special characters called wildcards and they can be used to select groups of files. For example, usingcp * my_directory/
to copy all files in the current working directory into another directory whilecp w*.txt my_directory/
selects all files in the working directory starting with “w” (prefix) and ending with “.txt” (suffix), and copies them to my_directory/.mv
command moves files without making a copy unlikecp
. To move a file into a directory, use mv with the source file as the first argument and the destination directory as the second argument. For example,mv `my_file.txt into my_directory/'
. It can also be used to move multiple files e.gmv my_file_1.txt my_file_2.txt my_directory/
.mv
can also be used to change file names e.gmv file_original.txt file_renamed.txt
rm
deletes files and directories permanently e.grm unwanted_file.txt
.rm -r
modifies the behavior of therm
command. The-r
stands for “recursive,” and it’s used to delete a directory and all of its child directories e.grm -r unwanted_directory
.
Input and Output Redirection
Redirection reroutes standard input, standard output, and standard error to or from a different location.
Standard input, abbreviated as
stdin
, is information inputted into the terminal through the keyboard or input device.Standard output, abbreviated as
stdout
, is the information outputted after a process is run.Standard error, abbreviated as
stderr,
is an error message outputted by a failed process.>
command redirects the standard output to a file. It overwrites all original contents.>>
command redirects the standard output to a file without losing the original text.<
takes the standard input from the file on the right and inputs it into the program on the left.|
is a pipe and takes the standard output of the command on the left, and pipes it as standard input to the command on the right.sort
takes the standard input and orders it alphabetically for the standard output (it doesn’t change the file itself).uniq
stands for “unique.” It filters out adjacent, duplicate lines in a file.grep
stands for “global regular expression print.” It searches files for lines that match a pattern and then returns the results. It is also case sensitive.grep -i
enables the command to be case insensitive.grep -R
searches all files in a directory and outputs filenames and lines containing matched results. -R stands for “recursive”.sed
stands for “stream editor.” It accepts standard input and modifies it based on an expression, before displaying it as output data.
Configuring the Command Line
A command line is a tool that is used often, to manage files in a project (move, delete, re-organize, replace, search etc). In software development, configuration of command line is necessary as this gets really busy due to the numerous things that are done on it. You can run commands directly from your command line or make use of development softwares like Git.
Basic Command Lines
nano hello.txt
is used to open the nano text editor.
ENVIRONMENTAL VARIABLES are variables that can be used across commands and programs and hold information about the environment.
env
command stands for “environment,” and returns a list of the environment variables for the current user.
PS1
is an environment variable that defines the makeup and style of the command prompt.
HOME variable is an environment variable that displays the path of the home directory~
.
~/.bash_profile
is where environment settings are stored. You can edit this file with nano.
PATH
is an environment variable that stores a list of directories separated by a colon.
PATH returns a colon :
separated list of file paths. It is customized in advanced cases.
The nano editor is a command line text editor used to configure the environment.
export VARIABLE="Value"
sets and exports an environment variable.
HOME
is the home directory. It is usually not customized.
USER
is the name of the current user.
PS1
is the command prompt.
./script.sh
used to run script.
Automating with Bash Script
Bash scripting is a great way to automate repetitive tasks. There are a few steps to follow to ensure your computer is able to find and execute your bash scripts.
The beginning of your script file should start with #!/bin/bash
on its own line as this tells your computer which type of interpreter to use for the script.
chmod +x
is use to give an execute permission to allow a file to run the file every time it opens to load it's configuration .
Variables are declared by setting the variable name equal to another value. They are accessed using a dollar sign (echo $greeting)
.
Conditionals
Conditionals are used to control which set of commands within the script run.
if [ $index -lt 5 ]
then
echo $index
else
echo 5
fi
If
is to start the conditional.
Followed by the [ ]
There should be a space between a bracket and the conditional statement
then
begins the code that will run if the condition is met.
else
begins the code that will run if the condition is not met.
the conditional is closed with a backwards if
, fi
.
Bash Script Comparison Operators
-lt
Is a less than operator.-eq
Equal-ne
Not equal-le
Less than or equal-lt
Less than-ge
Greater than or equal-gt
Greater than-z
is null==
Equal!=
Not equal
Loops
Loops can be done in 3 ways; using a for loop, while loop and until.
- For loop is used to iterate through a list and execute an action at each step for example
#!/bin/bash first_greeting="Nice to meet you!" later_greeting="How are you?" greeting_occasion=0 while [ $greeting_occasion -lt 3 ] do if [ $greeting_occasion -lt 1 ] then echo $first_greeting else echo $later_greeting fi greeting_occasion=$((greeting_occasion + 1)) done
- while loop loops keep looping while the provided condition is true for example;
while [ $index -lt 5 ]
do
echo $index
index=$((index + 1))
done
- until also loops until the condition is true for example;
until [ $index -eq 5 ]
do
echo $index
index=$((index + 1))
done
Inputs
Inputs are used to access data external to the bash script file itself. This is done by prompting the user for an input, and for this the read input is used and it looks something like this;
echo "Guess a number"
read number
echo "You guessed $number"
We can also access external data by having the user add input arguments when they run your script. These arguments are entered after the script name and are separated by spaces. For example:
saycolors red green blue
"$@"
is used when the script needs to accept an indefinite number for example
for color in "$@"
do
echo $color
done
- Aliases allows file(s) to allow calling your scripts without the full filename for example, if we have our
saycolors.sh script
, we can alias it to the word saycolors using the following syntax:
alias saycolors='./saycolors.sh'
A standard input argument can also be added to your alias, for example, we can write like this alias saycolors='./saycolors.sh "green"'
if we want green to always be included as the first input to saycolors
.