Bash Basic Syntax
# Learning Resources:
Skimming and pulling creating notes for TILs:
- Bash Scripting Tutorial for Beginners (FreeCodeCamp Video)
- Google Style Guide
- https://linuxcommand.org/lc3_writing_shell_scripts.php
# Hello World Scripting
What is the line that all bash scripts need to start with, and why? ? A shebang (`#!) declaration is required, so that the system knows what program/shell needs to be used in the running of the script.
| |
To check version on debian/ubuntu/pop:
| |
What permission change is required of bash script files to allow it’s functionality? Explain the octal permission system and the common argument for sharing a script.
?
Bash scripts need to have it’s permission set to executable for the relevant principal executing.
A common note, is to use chmod 755 to only allow others read and execute permissions.
| |
# Variables and Conditional Arguments
What syntax is used to receive user input to a variable?
?
read
e.g
| |
What are positional arguments and what is the 0-th position reserved for?
?
- Refers to the arguments provided to shell, by commands.
- The
0-th argument is reserved for the shell.
What is the syntax to make use of positional arguments?
?
echo $1 $2 etc.
| |
# Output Redirection
How do you redirect script output?
?
Redirects are done using “>”.
What is the difference between CTRL + d and CTRL + c in interacting with terminal? ?
- CTRL + c is an interrupt.
- CTRL + d is a End of File signal, often this will log user out of terminal.
What syntax allows appending text to an existing file, instead of overwriting it? ?
| |
# Input Redirection
What is a heredocs, what is it commonly used with and Why?
?
Heredocsare multi-line command blocks.- Commonly used with
cat. - This is as it allows for the printing of manipulated output/text to screen or to file by redirect.
What is syntax for use of Heredocs?
?
Heredocsuse (<<) syntax with aDELIMITER.- This has the effect of redirecting input from a “file” that users write into terminal, separated by lines and defined by the delimiters
- Example:
- Here, we are passing two lines of text containing an
environment variable and a command to
catusing a here document.
| |
| |
- or through use with “
sort”:
| |
What does <<< syntax denote?
?
- These are
herestrings, similar to heredocs but instead placing the input to be redirected on the same line as the command. Herestringsneed to be within double-quotes:
| |
| |
What are the different syntax methods to provide input to a command? ?
- Redirect
< - Heredocs
<< - Herestrings
<<<
# Comments
What is the syntax for comments in bash? ?
| |
# Piping
What is piping? What is it’s syntax? ? Piping is the practice of feeding the output of one command, into another.
| |
This will only show ls results will hence only show results that contain the string “bash”.
# Test Operators
What syntax refers to the exit code of the last command executed? How would you print this to terminal? ?
- Syntax to refer to last command’s exit code / return value is
$? - We can print this to terminal with
echo $?
What is syntax to incorporate test/evaluation operators to a command? ?
- We can use square brackets e.g
[ hello = hello].- This would evaluate to the successful exit code
0.
- This would evaluate to the successful exit code
# Conditionals and Test Expressions (if/elif/else)
How would you syntactically write an if comparison statement for a bash script, referencing the first provided argument to the command?
?
Example:
| |
What is the equivalent to len(x) in bash?
?
${#x}
How do you close if statements in bash?
?
fi.
What is the syntax for case statements, noting start, end and new statement syntax. ?
| |
# Arrays
How do you declare arrays in bash?
?
MY_FIRST_ARRAY=("Apple" "Orange" "Banana")
How would you index into the first item in a bash array? How would you print the WHOLE array? ?
- Specific index:
echo {MY_FIRST_ARRAY}[0] - Entire array:
echo {MY_FIRST_ARRAY}[@]
How do you add items to arrays in bash? What happens when you ?
- You place new values into unused indexes.
How do you remove items from arrays in bash?
?
You use the unset command.
| |
Does bash support slicing?
?
Yes - for example, you can get a range of elements using ${array[start_index:end_index]}:
| |
# Bash Functions
What is bash syntax for defining and calling a function? ? Definition:
| |
Example script calling function:
| |
How can you pass arguments within script and from terminal, to functions? ?
- Arguments can only be received from the terminal (through
$1etc.)- Bash does not support function arguments, like other languages. Example:
| |
- Note that this example is scoped within the same script, and doesn’t take arguments from terminal.
# Parameter and expansion
The hash symbol # and the percent signs `
${variable#pattern}- When you use
${variable#pattern}, it removes the shortest match ofpatternfrom the beginning of the value stored invariable.
- When you use
`${variable
- When you use `${variable
Here’s a brief example to illustrate further:
| |
# Compressing a folder:
| |
-c: Create a new archive.-z: Compress the archive using gzip.-v: Verbose mode, which displays the files being added to the archive.