Basics of Terminal
Executing .sh file in terminal:
<path> <filename>.shCreating a variable:
variable_name=valueAccessing the value of a variable:
${variable_name}Assigning the output of a command to a variable:
echo $variable_namePerforming arithmetic operations in shell script:
variable_name=$((expression))Exporting a variable:
You can export variables in your .bashrc file to make them available to other programs and scripts. This will create a process specific environment variable. You can export a variable by using the following syntax:
export variable_name=valueLooping Through a List of Values in Shell Script
for variable_name in value1 value2 value3Looping Through a List and Executing Commands
for variable_name in value1 value2 value3;
do echo $variable_name;
doneConditional Statements in Shell Script
if [ condition ];
then echo "condition is true";
else echo "condition is false";
fiUseful Shell Commands
echo "Hello World"- prints βHello Worldβ to the terminalread variable_name- reads input from the user and stores it in a variablels- lists all files and directories in the current directorycd directory_name- changes the current directory to the specified directorypwd- prints the current working directorymkdir directory_name- creates a new directorytouch file_name- creates a new filerm file_name- removes a filermdir directory_name- removes an empty directoryrm -r directory_name- removes a directory and all its contentscp file1 [pathOf file2]- copies file1 to file2mv file1 [pathOf file2]- moves file1 to file2rename old_name new_name- renames a filecat file_name- displays the contents of a filechmod permissions file_name- changes the permissions of a file
Special Variables in Shell Script
$0- the name of the script$1, $2, $3, ...- the first, second, third, β¦ argument passed to the script$#- the number of arguments passed to the script$@- all the arguments passed to the script$?- the exit status of the last command$$- the process ID of the current shell
Conditional Operators
-eq- equal to-ne- not equal to-gt- greater than-lt- less than-ge- greater than or equal to-le- less than or equal to
Logical Operators
&&- and||- or!- not
About the .bashrc File
The .bashrc file is a script executed whenever a new terminal is opened. It can be used to set environment variables, define functions, and customize the shell prompt. It is located in the home directory and can be edited using text editors like nano or vim.
PS in .bashrc File
PS1- variable can contain special characters that represent information like the current working directory, the username, and the hostname.PS2- variable defines the prompt that is displayed when the shell expects more input.PS3- variable defines the prompt that is displayed when the select command is used.PS4- variable defines the prompt that is displayed when the shell is in debugging mode.
Aliases in .bashrc
Aliases are shortcuts that you can define in your .bashrc file to make it easier to run commands. You can define an alias by using the following syntax:
alias alias_name='command'
Functions in .bashrc
Functions can be defined for reusable code blocks using:
function_name() {
# code goes here
}Sourcing the .bashrc file.
After you make changes to your .bashrc file, you need to source it to apply the changes to your current shell session. You can source the .bashrc file by running the following command:
source ~/.bashrcPriority levels of checking commands in shell script:
- Aliases
- Functions
- Built-in commands
- Hash Tables
- Executable files / PATH variable
What is Shebang?
A shebang (also called a hashbang) is the character sequence #! followed by the path to an interpreter, placed at the very top of a script file. It tells the operating system which interpreter to use to run the script.
the script must also have execute permissions (e.g., using chmod +x scriptfile).
#!/usr/bin/env node is a common shebang for Node.js scripts, as it uses the env command to find the Node.js interpreter in the userβs PATH.