Shell environment
Users, Groups, and Processes
Linux was created with the idea that multiple users can be using a computer at the same.
- The
whoami
command prints out the name of the currently logged in user. - The
users
command prints out all the users that exist on the computer. - The
pinky
command prints out more information about all the users. - The
groups
command prints out all the groups you are a part of. - The
ps
command prints out the processes you are currently running. - The
top
command shows a constantly updating list of processes running and who is running them along with other helpful information such as CPU or memory usage, time running, etc.
Super Users are system administrators who have permissions to do essentially anything on a system including provisioning permissions for normal users. To run a command as a super user use the sudo
(“super user do”) command which gives you administrator privileges on that command you type after it.
The /usr
directory stands for Universal System Resources which contains the /bin
or binary directory.
You can reference your last command in the shell by typing !!
which means that you can type sudo !!
when you want to give the command you ran earlier (that may have failed) super user permissions.
bashrc, bash_profile, and $PATH
.bash_profile
is a shell script that runs on login to the shell. The bash
command launches a new login shell. .bashrc
file is similar to the .bash_profile
file, but instead of only running on every login shell, it runs on every new shell, login or not. When making a change to either .bashrc
or .bash_profile
, instead of reloading the shell (logging in and out again), run source .bashrc
which runs the commands in the file line-by-line.
$PATH
is a special alias (variable name) that specifies directories for executable programs such as /bin
. Programs that read the $PATH
alias expect :
delimiters. Make sure to leave no spaces on either side of the equal sign and quotes around the command.
Permissions
The permissions of each file and directory in the current directory appear in the first column of the output from the ls -l
command in the format d rwx rwx rwx
:
- The first character is either a d if the item is a directory, or nothing if it isn’t.
- The next three groups are in the order of owner (u), group (g), and others (o).
- Read permissions are for operations such as the
cat
command, which only involves reading data. - Write permissions are for operations like
vim
oremacs
for making changes to the file/directory. - Execute permissions are for running files that can be executed such as bash shell scripts.
- Read permissions are for operations such as the
- Each of these groups can have up to three different permissions: read (r), write (w), and execute (x).
The chmod
command can change permissions.
chmod u+x [filename]
gives the owner execute permissions.chmod go-rw [filename]
takes away the permission to read and write from the group and others.- The other method is by using the NNN octal codes.
- Each is a group(owner, group, others) and the number can be from 0-7.
- +4 is read, +2 is write and +1 is for execution.
chmod 444 [filename]
gives read permissions to every group.chmod 600 [filename]
gives read and write permissions to only the owner.
Directory permissions work differently.
- Read means whether or not they can run
ls
on the directory. - Write determines if you can add, delete, or modify files in the directory.
- Execute is whether someone can
cd
into a particular directory.
The umask
command sets the default permissions for newly created files. umask 0022
gives the owner full permissions and the group and others only read and execute permissions.
Our first script
A shell script is a series of shell commands run from top to bottom. The most basic one looks like this:
#!/bin/bash
echo "Hello World!"
The #!bin/bash
is called a “shebang”. This tells your computer which “shell” should run the program; for this class, we’ll use bash
.
Subsequent lines are run one after another. Note that if your lines cd
to new directories or create new variables, that doesn’t carry over to the “parent shell”.
#!/bin/bash
mkdir bestfolder
cd bestfolder
touch bestfile
For example, the above shell script:
- will create a new folder called
bestfolder
- will create a new file called
bestfile
- will not move the shell that ran this script to
bestfolder
Variables
To create variables, use the =
:
color="red"
- don’t include spaces between the
=
symbol;color = "red"
will not work - by default, we use
"
. they are technically optional, but helpful when the input has spaces (e.g.color="sky blue"
works whilecolor=sky blue
doesn’t) - variables are case-sensitive (
color
is not the same asCOLOR
)
To reference a variable, use $
:
color="red"
echo $color
echo "my fave color is $color"
- within double quotes, variables are “expanded” to their value
- within single quotes, strings are interpreted literally;
echo 'My favorite color is $color'
would printMy favorite color is $color
- if a variable name is not found, bash does not throw an error; it just uses an empty string
Special Variables: Arguments
Bash “pre-fills” some variables for you that are related to the script’s arguments:
$1
is the first argument,$2
is the second, …$0
is the filename of the script$#
is the number of arguments provided (not counting the filename)$@
is all of the arguments at once
Exit Codes
The $?
variable is also pre-filled by bash and tells you the exit code for the previous command.
Usually, a 0
indicates that the command was successful, while a non-zero code means there was a failure.
You can make a script exit with exit
, e.g. exit 1
.
For Loops
Bash has a special syntax for for
loops, with the for
, in
, do
, and done
keywords. Here are some examples iterating over numbers, files, and arguments - all in one syntax style. Note how the first two use command substitution - the $()
- to “capture” the output of a command.
#!/bin/bash
for i in $(seq 1 4); do
echo $i
done
#!/bin/bash
for file in $(ls); do
echo $file
done
#!/bin/bash
for arg in $@; do
echo $arg
done
You can also put the do
on a separate line:
#!/bin/bash
for arg in $@
do
echo $arg
done
Arithmetic
To do arithmetic, we can either use let
or $(())
:
a=1
let b="$a + 2"
c=$(( $a + $b ))
If Statements
Bash has a very specific syntax for if
statements - be very mindful of the spacing with the [
and ]
!
Here are some examples:
if [ -n "$NAME"]; then
echo 'Variable $NAME exists'
fi
if [ $a -lt 10 ] && [ $a -gt 5 ]; then
echo "variable a is between 5 and 10"
fi
if [ $1 -lt $2 ]; then
echo "arg1 is less than arg2"
elif [ $1 -eq $2 ]; then
echo "arg1 equals arg2"
else
echo "arg1 is greater than arg2"
fi
Arithmetic Operators
Operator | Description |
---|---|
-gt | greater than |
-ge | greater than or equal to |
-lt | less than |
-le | less than or equal to |
-eq | equal to |
-ne | not equal to |
Boolean Operators
Operator | Description |
---|---|
if [ expr1 ] && [ expr2 ]; then | boolean AND |
if [ expr1 -a expr2 ]; then | boolean AND |
if [ expr1 ] || [ expr2 ]; then | boolean OR |
if [ expr1 -o expr2 ]; then | boolean OR |
if [ ! expr1 ]; then | boolean NOT |
String Operators
Operator | Description |
---|---|
= | equal to |
!= | not equal to |
-z | is empty |
-n | is non-empty |
File and Directory Operators
Operator | Description |
---|---|
-f | file exists |
-d | directory exists |
-r | file is readable |
-w | file is writable |
-x | file is executable |