Skip to Content
πŸŽ‰ Welcome to my notes πŸŽ‰

Basics of Terminal

Executing .sh file in terminal:

<path> <filename>.sh

Creating a variable:

variable_name=value

Accessing the value of a variable:

${variable_name}

Assigning the output of a command to a variable:

echo $variable_name

Performing 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=value

Looping Through a List of Values in Shell Script

for variable_name in value1 value2 value3

Looping Through a List and Executing Commands

for variable_name in value1 value2 value3; do echo $variable_name; done

Conditional Statements in Shell Script

if [ condition ]; then echo "condition is true"; else echo "condition is false"; fi

Useful Shell Commands

  • echo "Hello World" - prints β€œHello World” to the terminal
  • read variable_name - reads input from the user and stores it in a variable
  • ls - lists all files and directories in the current directory
  • cd directory_name - changes the current directory to the specified directory
  • pwd - prints the current working directory
  • mkdir directory_name - creates a new directory
  • touch file_name - creates a new file
  • rm file_name - removes a file
  • rmdir directory_name - removes an empty directory
  • rm -r directory_name - removes a directory and all its contents
  • cp file1 [pathOf file2] - copies file1 to file2
  • mv file1 [pathOf file2] - moves file1 to file2
  • rename old_name new_name - renames a file
  • cat file_name - displays the contents of a file
  • chmod 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 ~/.bashrc

Priority levels of checking commands in shell script:

  1. Aliases
  2. Functions
  3. Built-in commands
  4. Hash Tables
  5. 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.

Last updated on