Skip to content

Instantly share code, notes, and snippets.

@pkutaj
Created March 12, 2024 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkutaj/7323155eaa93f38403fe484588ddc58a to your computer and use it in GitHub Desktop.
Save pkutaj/7323155eaa93f38403fe484588ddc58a to your computer and use it in GitHub Desktop.
NR Keyword Description Example
01 if Introduces a conditional statement to execute code based on a condition. if [ $x -gt 0 ]; then echo "Positive number"; fi (checks if x is positive)
02 then Follows the if keyword and marks the beginning of the code to execute if the condition is true. Refer to if example.
03 else Provides an optional block of code to execute if the condition in the if statement is false. if [ $x -gt 0 ]; then echo "Positive"; else echo "Non-positive"; fi (prints based on x value)
04 elif Introduces an additional condition to check within an if statement structure. if [ $x -gt 0 ]; then echo "Positive"; elif [ $x -lt 0 ]; then echo "Negative"; else echo "Zero"; fi (checks for positive, negative, or zero)
05 fi Marks the end of an if, else, or elif block. Refer to if example.
06 case Provides a multi-way branching statement based on pattern matching. case $choice in a) echo "Option A";; b) echo "Option B";; esac (checks the value of choice)
07 esac Marks the end of a case statement block. Refer to case example.
08 for Introduces a loop that iterates a set number of times or over a list of items. for i in {1,2,3}; do echo $i; done (loops from 1 to 3)
09 select Creates a menu-driven loop for user selection. select name in John Mary Peter; do echo "Hello, $name!"; break; done (prompts user to choose a name)
10 while Creates a loop that continues as long as a condition is true. while [[ $count -lt 5 ]]; do echo $count; count=$((count+1)); done (loops until count reaches 5)
11 until Creates a loop that continues until a condition becomes true. until [[ $file -f ]]; do sleep 1; done ; echo "File found!" (loops until file exists)
12 do Marks the beginning of the code block to be executed within a loop or conditional statement. Refer to for, while, and until examples.
13 done Marks the end of the code block for a loop or conditional statement. Refer to for, while, and until examples.
14 in Used within loops to specify the list of items to iterate over. Refer to for example.
15 function Defines a reusable block of code that can be called with arguments. function greet { echo "Hello, $1!"; } ; greet World (defines and calls a greeting function)
16 time Executes a command and reports the elapsed time, user CPU time, and system CPU time used. time sleep 5 (measures the time spent sleeping for 5 seconds)
17 { } Delimiters used to define a compound command, which groups multiple commands into a single unit. { echo "Line 1"; echo "Line 2"; } (groups two echo commands)
18 ! Logical NOT operator that inverts the truth value of an expression. if [[ ! $file -e ]]; then echo "File does not exist"; fi (checks if the file doesn't exist)
19 [[ ]] Provides a more flexible way to write conditional expressions compared to test (brackets). Refer to if and while examples using [[ ]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment