Skip to content

Head first bash

First glance

#!/bin/bashUsed to tell the operating system the path it should use to interpret the file.
bash file.shUsed to execute the script in the terminal.
./file.shUsed to execute the script if it is executable.
#Used to make comments in the script.
&&logical AND operator.
| |logical OR operator.
$#Used to expands the number of arguments passed to the script.
$0Used to expands to the name of the shell.
$1, $2Used as an input param­eter that you can add when running script.
exit [0-255]Used to exit the script and return the number from 0 to 255.
$Used for parameters and variables.
()Used for running commands in a subshell.
$()Used to save the output of commands.
(())Used for arithmetic.
$(())Used to retrieve the output of arithmetic expressions.
[]Used in filename expansion and string manipulation.
<( )It is very similar to a pipe and used for process substitution.
{ }Used to expand sequences.
${ }Used for string manipulation and variable interpolation.
|Used to run multiple commands together.|
>Used to send output to a file.
>>Used to append output to a file.
;Used to separate multiple commands.
<Used to get input from a file.
~Expands to the home directory.
~/.bashrcRead by every non-login shell.
/etc/profileExecuted automatically at login.

File Test Operators

OperatorsDescription
-eTo test if a file exists.
-fTo test if a given file is a regular file.
-dTo test if the file is a directory.
-bTo test if the file is a block device.
-sTo test if the file is not zero sizes.
-LTo test if the file is a symbolic link.
-STo test if the file is a socket.
-rTo test if the file has read permission.
-wTo test if the file has write permission.
-xTo test if the file has execute permission.
-gSet group id on file or directory.
-uSet user id on file or directory.
-kSet a sticky bit.
-OYou are the owner of the file.
f1 -nt f2file f1 is newer than f2.
f1 -ot f2file f1 is older than f2

Comparison Operators

Integer Operators
-eqis equal to
-neis not equal to
-gtis greater than
-geis greater than or equal to
-ltis less than
-leis less than or equal to
String Operators
OperatorsExplanation
=is equal to
==is equal to
!=is not equal to
<less than
<=is less than or equal to
>greater than
>=is greater than or equal to
-zstring is null
-nstring is not null

Regular Expressions

ExpressionsExplanation
.Matches any single character.
?The preceding item is optional and will be matched, at most, once.
*The preceding item will be matched zero or more times.
+The preceding item will be matched one or more times.
{N}The preceding item is matched exactly N times.
{N,}The preceding item is matched N or more times.
^Matches the empty string at the beginning of a line.
$Matches the empty string at the end of a line.
[a-d]Matches any one character in the range a-d.

Loops and Conditions

LoopsExplanation
if then fiUsed to test a condition.
if then else fiUsed to test a condition and use a fallback if the test fails.
if then elif else fiUsed to test a condition and use a fallback if all tests fail.
for do doneIterate over a list of values.
while do doneUsed to performs a given set of commands an unknown number of times as long as the given condition evaluates to true.
until do doneUsed to execute a given set of commands as long as the given condition evaluates to false.
sleep timeWait for a specified amount of time before continuing through the script.
breakUsed to exit from the while or for loop but continue the rest of the script.
continueUsed to skip the current iteration of a loop and continue to the next iteration of the loop.

Bash Arrays and Functions

ArrayExplanation
array=(“elements of array”)Used to create an array of strings.
${array[0]}Used to get the first element of the array.
${array[*]}Used to get all values in the array.
${array[-1]}Get the last value in the array.
${array[@]}Expand all of the array elements.
shiftMove argument from $2 to $1.
function()Used to define a function.
aliasUsed to list all aliases defined in the current session.
alias alias=’any command’Used to define an alias.

Common Utilities and Switches

CommandsExplanation
ls -lList files by type and permission.
ls -aList all files, including hidden files.
pwdDisplay current working directory.
whoamiWho you are logged in as.
lastDisplay last user logins information.
find /home -name *.txtSearch all text files in /home directory.
find . -size 10k -printFind all files greater than 10k in the current directory.
egrep “(foo|bar)” file.txtFind the words foo and bar in file.txt.
sed s/foo/bar/g file.txtFind the word foo and replace it with a bar in file.txt.
locate file.txtFind the location of the file.txt quickly.
grep foo file.txtSearches the word foo in file.txt.
ps -efTo check all running services.
netstat -antTo check all network connections.
netstat -entTo check established network connections.
ifconfigTo check all network interfaces, IPs, and Mac addresses.
pingUsed to check host reachability.
nslookupUsed for DNS query.
sshUsed to login remote Linux system.
scp -r dir user@remote-ip:/opt/Copy all files and directories recursively from the local system to a remote system.
scp -r user@remote-ip:/opt/ dir/Copy all files and directories recursively from the remote system to a local system.
rsync -avz localdir user@remote-ip:/backupSynchronize files/directories between the local and remote systems.
df -hShows free and used space on mounted filesystems.
du -shShows total disk usage of the current directory.
free -mShow free and used memory and swap space.
lsofLists files opened by running processes.
chown user:group filenameChange the owner of the file and directory.
chmod ugo file.txtChange the user, group, and other permis­sions for file.txt.
kill pidKill any running process.
passwd usernameUsed to set or reset the user password.
topDisplay all running processes, memory usage, cpu usage in real-time.

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback: dig +short txt issue.imzye.com
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Buy Me a Coffee