Bash is vast and comes with various commands and several syntaxes. If you are a developer working with Bash scripting, you should be aware of all frequently used basic commands. You can use this sheet for quick reference.
Let’s get started with this bible for Bash scripting.
BASH BASICS
These are the basic commands for any Linux or script developer.
env – specifies all the environment variables |
echo $SHELL – specifies the shell that is currently is in use |
echo $BASH_VERSION – show the version of the bash you are using |
bash – starts the bash shell |
whereis bash – helps to locate the binary, source, and other details of a command |
which bash – display process or program is executed as ‘bash’ |
clear – it clears the content on the window and hides the displayed lines |
FILE COMMANDS
These commands will help you to play around with files and perform various tasks accordingly.
ls – display all the files present in current directory |
ls -l – display the long listing of all the files containing the size of the file, who owns the file and its access permission and when it was last modified |
ls -a – display all the files in ‘long format’ along with the hidden files |
ln -s <filename> <link> – it will create a symbolic link to the specified file |
readlink <filename> – it displays where a symbolic links points to |
tree – it displays all the directories and subdirectories in a readable file tree |
touch <filename> – it will create and modify the mentioned file |
cat <filename> – it displays the file raw content |
tail <filename> – it will show the last lines of file |
vim <filename> – the specified will open in VIM text editor, will create that file if it doesn’t exist |
mv <filename1> <dest> – moves a file to destination |
cp <filename1> <dest> – it will copy the file to the specified destination |
rm <filename> – it will remove the specified file |
find . -name <name> <type> – it will look for the specified file or a directory in the current directory and all its sub-directories |
diff <filename1> <filename2> – it will compare the two mentioned files, and their differences |
wc <filename> – it will display the number of lines, words and characters in a file. |
rev – it will reverse the string characters |
chmod -options <filename> – allows you to change the read, write, and execute permissions |
gzip <filename> – it compresses the specifed file using gzip algorithm |
gunzip <filename> – it uncompresses the specified file compressed by gzip |
gzcat <filename> – you can check the gzipped file without actually gunzipping it |
lpr <filename> – it will print the specified file |
lpq – it will look for the printer queue |
lprm <jobnumber> – allows you to remove anything from the printer queue |
dvips <filename> – it will display the .dvi files |
grep <pattern> <filenames> – it will look for the string in the files |
grep -r <pattern> <dir> – it will search recursively for a specified pattern in directory |
head -n file_name | tail +n – it will display the nth line from the mentioned file. |
DIRECTORY COMMANDS
These commands allow you to work with directories and subdirectories available in the system.
mkdir <dirname> – it creates a new directory |
rmdir <dirname> – it removes an empty directory |
rmdir -rf <dirname> – it will remove a non-empty directory |
mv <dir1> <dir2> – it will rename the specified directory from <dir1> to <dir2> |
cd – it will change the directory to home |
cd .. – it will change the directory to the parent directory |
cd <dirname> – it will change the directory to the mentioned dirname |
cp -r <dir1> <dir2> – it will copy all the contents of dir1 to <dir2> |
pwd – it specified the current working directory |
cd ~ – it will change the directory to home. |
cd – – it will change the current working directory to previous working directory |
VARIABLES
With the help of variables, you can easily handle even more complex tasks.
varname=value – you can define a variable |
echo $varname – you can check the value of the variable |
echo $$ – it will display the process ID of the current shell |
column -t <filename> – it will show the details in pretty columns |
let <varname> = <equation> – it will carry out the mathematical calculation using arithmetic operators |
export VARNAME=value – it will define the environment variable |
array[0]=valA – method 1- defining an array |
array[1]=valB |
array=([2]=valC [0]=valA [1]=valB) – method 2- of defining the array |
array=(valA valB valC) – method 3- defining an array |
declare -a – define variables as arrays |
declare -f – it will use the function names only |
declare -F – it will show the function names without definitions |
declare -i – define variables as integers |
declare -r – it will define the read-only variables |
${- varname} – it will return the length of the variable’s value as a character string |
SSH, SYSTEM INFO & NETWORK COMMANDS
These commands will allow you to work with the network, making the required settings, checking details of the available network, user, space on the system, and others.
ssh user@host – you can connect to the host as the user |
ssh -p <port> user@host – you can connect to host on the specified port as user |
ssh-copy-id user@host – it allows you to add ssh key to the host for the user for passwordless login |
whoami – it will display the username |
su <user> – it lets you switch to a different user |
su – – you can switch to root (sudo su -) |
sudo <command> – it lets you run the command as the root user |
passwd – allows you to change the password |
date – display the current date and time |
cal – it will display the month’s calendar |
uptime – it will display the current uptime |
w – uit will show whois online at the time |
finger <user> – it will specify the information about the specified user |
uname -a – it will display all the kernel-related information |
man <command> – it will display the manual for mentioned command |
help – it will display the documentation regarding the built-in commands and functions |
df – it will display the disk usage |
du <filename> – it will display the disk usage of the mentioned files and directories in the filename |
last <yourUsername> – it will display all your last logins |
ps -u your username – it will display all your processes |
kill <PID> – it allows you to kill the specified processes with the ID |
killall <processname> – it lets you kill all processes with the specified process name |
fg <job> – it will bring the specified job to the foreground |
ping <host> – it allows you to ping the host and outputs results |
whois <domain> – it will let you check whois information for a specified domain |
dig <domain> – you will get all the DNS-related information for the domain |
dig -x <host> – it will reverse the lookup host |
wget <file> – it allows you to download the specified file |
COMMAND-LINE Processing Cycle
Below is the command-line processing cycle. The default order that is being followed for the command lookup is functions, built-ins, scripts, and executables. You can use the following built-ins to override the order:
command – it will remove the alias and function lookup. |
builtin – it will look up only built-in commands that ignore the functions and commands available in the PATH |
enable – it will enable and disable the shell built-ins |
INPUT/OUTPUT Redirectors
These commands will help you redirect the error, input, and output to a specified file separately for debugging purposes.
< file – take the standard input from the specified file |
> file – provide the standard output to the specified file |
>> file – move the standard output to file |
>|file – force the standard output to file |
n>|file – force the output to file from file descriptor n |
<> file – use the file as both standard input and output |
n>file – move the specified file descriptor n to file |
n<file -file descriptor n from the file |
n>>file – file description n to file. It will append to file if it already exists |
n>& -duplicate standard output to file descriptor n |
n<& – duplicate the standard input from file descriptor n |
<&- -close the standard input |
>&- – complete the standard output |
n>&- – close the output from file descriptor n |
Process Handling
Using these commands, you can handle the run, stop, and other processes available on your system.
myCommand & – this command will run the job in the background and prompts back the shell |
jobs – it will display all the jobs |
fg – it will bring the background job into the foreground |
fg %string – it will bring the job whose command begins with the string |
kill -l – it will return all signals on the system |
kill PID – it will terminate the process with specified PID |
kill -s SIGKILL 4500 – it will send a signal to terminate the process |
ps -a – it will select all the processes with a tty except session leaders |
disown <PID|JID> – it will remove the process from the list of jobs |
wait – it will allow you to wait until all background jobs have finished |
Comparisons
These are the comparison operators that allow you to compare strings, files, and numbers.
– STRINGS |
str1 == str2 – it will matches the str1 with str2 |
str1 != str2 – str1 is not equal str2 |
str1 < str2 – str1 is less than str2 (in case of strings, it is alphabetically) |
str1 > str2 – str1 is greater than str2 (in case of strings, it is alphabetically) |
str1 \> str2 – str1 will be sorted after str2 |
str1 \< str2 – str1 will be sorted before str2 |
– FILES |
-a file – specify that the file exists |
-d file – specify that the file exists and is a directory |
-e file – specify that the file exists |
-f file – specify that the file exists |
-r file – specify that the user has the read permission on the specific file |
-s file – specify that the file exists and is not empty |
-w file – specify that the user has the write permission on the specific file |
-x file – specify that the user has the execute permission on file |
-O file – user own file |
– NUMBERS |
-lt – stands for less than |
-le – stands for less than or equal |
-eq – specifies the equal operator |
-ge – stands for greater than or equal |
-gt – stands for greater than |
-ne – stands for not equal |
Control Statements
These control statements are used to control the flow of the execution of the code. There are different types of control statements.
if condition then code [elif condition then code…] [else code] fi for x in {1..10} do code done for name [in list] do Code that can use $name done for (( initialisation ; ending condition ; update )) do Code… done case expression in pattern1 ) code; pattern2 ) code; esac select name [in list] do Code that can use $name done while condition; do code done until condition; do code done |
Conclusion
If you are a bash script developer, this cheat sheet will be beneficial for your quick reference to various syntax and commands. We have summed up the most frequently used commands in this sheet to help you prepare for your upcoming interviews. If you have any questions, ask us in the comments section.