1 Introduction to Bash coding
Bash is widely used shell on Linux systems. Most major Linux distributions include Bash as the default shell. It is also available on macOS and Windows (via WSL or Git Bash).
This is a simple example of a Bash script that prints “Hello, World!” to the console:
echo "Hello, World!"To run this script, you can save it to a file named hello.sh, give it execute permissions using chmod +x hello.sh, and then execute it with ./hello.sh.
create the file hello.sh and add the following content:
touch ./files/hello.shnano ./files/hello.sh
#!/bin/bash
echo "Hello, World!"for saving in nano, press CTRL + X, then Y, and then ENTER.
Make the script executable:
chmod +x ./files/hello.shRun the script:
./files/hello.sh
# Output: Hello, World!This will output “Hello, World!” to the console.
1.1 What is Bash?
Bash (Bourne Again SHell) is a command-line interpreter that allows users to interact with the operating system by executing commands and running scripts. It is widely used for system administration, automation, and programming tasks.
# Check the version of Bash
bash --versionFor checking your default shell, you can use the following command:
echo $SHELLOne of the other variables in the environment is PATH, which lists directories that the shell searches for executable files. You can view it with:
echo $PATH1.2 Pipes and redirection
cd ./files/ExerciseFiles
cat lorem.txtFor clear the screen:
clearFor scrolling the output we can pass it to less:
cat lorem.txt | lessand for exiting less, press q.
Now we can count the number of lines and words in the file using wc (word count):
cat lorem.txt | wc
# Output: 45 1853 12577This output indicates that the file contains 45 lines, 1853 words, and 12577 characters. Also we can use wc -l to count only the number of lines:
cat lorem.txt | wc -l
# Output: 45redirection of output to a file using >:
cat lorem.txt | wc -l > line_count.txt
cat line_count.txt
# Output: 45This command counts the number of lines in lorem.txt and writes the result to line_count.txt. If line_count.txt already exists, it will be overwritten.
we have different options for redirection: - >: Redirects output to a file, overwriting the file if it exists. - >>: Redirects output to a file, appending to the file if it exists. - 2>: Redirects standard error (stderr) to a file. - &>: Redirects both standard output (stdout) and standard error (stderr) to a file. In Bash, there are three standard streams: - Standard input (stdin): Stream 0 - Standard output (stdout): Stream 1 - Standard error (stderr): Stream 2 Here are some examples of using redirection:
ls ./files/ExerciseFiles > ./files/output.txt
cat ./files/output.txtls ./notexist 1> ./files/output.txt 2> ./files/error.txtBecause the directory ./notexist does not exist, the error message will be written to error.txt. and nothing will be written to output.txt.
‘<’ is used for input redirection, allowing a command to read input from a file instead of standard input. For example:
wc -l < ./files/lorem.txt
# Output: 45‘<<’ is used for here-document, allowing you to provide input directly within the script. For example:
wc -w << EOF
This is a sample text
with multiple lines.
EOF
# Output: 71.3 Bash builtin and other commands
echo is a built-in command in Bash that outputs the strings it is given as arguments. It is commonly used to display messages or the values of variables. For example:
echo "Hello, World!"
# Output: Hello, World!printf is another built-in command in Bash that provides more control over the formatting of the output compared to echo. It allows you to specify format specifiers for different data types. For example:
printf "Name: %s\nAge: %d\n" "Alice" 30
# Output:
# Name: Alice
# Age: 301.4 Brackets and braces in Bash
In bash we have Parantheses () , Brackets [] and Braces {}. Parantheses () are often used for enclosiing conditions and for function parameters. Braces {} are used for function bodies and objects, and Brackets [] are used for lists or array notation.
1.5 Bash expansions and substitutions
~: Tilde expansion, which represents the home directory of the current user. For example,cd ~will take you to your home directory.{...}: Brace expansion, which generates a list of strings based on a pattern. For example,echo file{1,2,3}.txtwill outputfile1.txt file2.txt file3.txt.${...}: Parameter expansion, which allows you to access and manipulate the value of variables. For example,name="Alice"; echo ${name}will outputAlice.$(...): Command substitution, which allows you to execute a command and use its output as part of another command. For example,echo "Today is $(date)"will output the current date.$((...)): Arithmetic expansion, which allows you to perform arithmetic operations. For example,echo $((3 + 5))will output8.
If i want to go back to my previous directory, I can use cd -:
cd /tmp
cd -
# Output: /home/username (or your previous directory)1.5.1 Brace expansion {}
Sequence expressions using braces {}:
echo {1..5}
# Output: 1 2 3 4 5
echo {a..e}
# Output: a b c d e
echo {1..10..2}
# Output: 1 3 5 7 9
echo {a..z..3}
# Output: a d g j m p s v y
echo {A..Z..4}
# Output: A E I M Q U Y
echo {0..100..20}
# Output: 0 20 40 60 80 100creating diferent files using brace expansion:
touch file{1..5}.txt
ls file*.txt
# Output: file1.txt file2.txt file3.txt file4.txt file5.txt
rm file*.txttouch file_{a..e}{1..3}.txt
ls file_*.txt
# Output: file_a1.txt file_a2.txt file_a3.txt file_b1.txt file_b2.txt file_b3.txt file_c1.txt file_c2.txt file_c3.txt file_d1.txt file_d2.txt file_d3.txt file_e1.txt file_e2.txt file_e3.txt
rm file_*.txtcreating directories using brace expansion:
mkdir dir{A..E}
ls -d dir*
# Output: dirA dirB dirC dirD dirE
rm -r dir*1.5.2 Parameter expansion ${}
Setting and accessing variables:
name="Alice"
echo "Hello, ${name}!"
# Output: Hello, Alice!
echo "Hello, ${name:0:3}!"
# Output: Hello, Ali!** Bash is sensitive to spaces around the = sign when assigning variables. For example, name = "Alice" will result in an error, while name="Alice" is correct. **
Substring extraction using parameter expansion:
greeting="Hello there!"
echo "${greeting:6}"
# Output: there!
# starting from index 6 to the endecho "${greeting:0:5}"
# Output: Hello
# starting from index 0 and taking 5 charactersreplacing substrings using parameter expansion:
text="The quick brown fox"
echo "${text/quick/slow}"
# Output: The slow brown fox
# replace the first occurrence of "quick" with "slow"
echo "${text//o/O}"
# Output: The quick brOwn fOx
# replace all occurrences of "o" with "O" 1.5.3 Command substitution $(...)
Using command substitution to capture the output of a command:
current_date=$(date)
echo "Today's date is: $current_date"
# Output: Today's date is: Mon Jun 10 12:34:56 UTC 2024 (example output)echo "The python version is: $(python3 --version)"
# Output: The python version is: Python 3.x.x (example output)1.5.4 Arithmetic expansion $((...))
Performing arithmetic operations using arithmetic expansion:
a=5
b=3
sum=$((a + b))
echo "The sum of $a and $b is: $sum"
# Output: The sum of 5 and 3 is: 8
difference=$((a - b))
echo "The difference of $a and $b is: $difference"
# Output: The difference of 5 and 3 is: 2
product=$((a * b))
echo "The product of $a and $b is: $product"
# Output: The product of 5 and 3 is: 15
quotient=$((a / b))
echo "The quotient of $a and $b is: $quotient"
# Output: The quotient of 5 and 3 is: 1
remainder=$((a % b))
echo "The remainder of $a divided by $b is: $remainder"
# Output: The remainder of 5 divided by 3 is: 2
exponentiation=$((a ** b))
echo "The result of $a raised to the power of $b is: $exponentiation"
# Output: The result of 5 raised to the power of 3 is: 125