Home

Awesome

GitHub stars GitHub forks GitHub license contributions welcome

Bash-scripts

A collection of Bash scripts for automating routine tasks and streamlining your workflow. From simple file renaming to more complex deployments, these Bash scripts have you covered.

Screenshot

About Bash

Bash (Bourne Again SHell) is an essential component of Unix-like operating systems. It's a powerful scripting language and command interpreter that has evolved from its predecessors in the Unix world. Here's an overview:

Historical Background

Purpose of Shell Scripts

Limitations of Bash

"Hello World" in Bash

A basic example of a Bash script is the famous "Hello World". It demonstrates how to output text to the console.

#!/usr/bin/env bash
echo "Hello world"

Executing a script

To execute a Bash script, you need to make it executable and then run it from the terminal. Here's how you can do it:

chmod u+x filename.sh  # Make the script executable
./filename.sh          # Execute the script

The Shebang

The shebang (#!) line at the beginning of a script is crucial for defining the script's interpreter. It's more than just a convention; it's an essential aspect of script execution in Unix-like systems. Here's an expanded view:

Example Shebang for Bash:

#!/usr/bin/env bash

Execution Contexts

Shebang in Other Languages

Variables

Variables in Bash are not just simple placeholders for values; they can be used in more complex ways:

declare -i var    # 'var' is an integer
declare -a arr    # 'arr' is an array
declare -r var2=5 # 'var2' is a read-only variable
function myFunc() {
    local localVar="value"
}

Command Line Arguments in Bash

Command line arguments in Bash scripts are accessed using special variables:

If Statements in Bash

If statements in Bash are crucial for decision-making processes based on conditions.

Basic Syntax:

if [ condition ]; then
  # commands
fi
if [ $i -eq 10 ]; then echo True; fi  # Integer comparison
if [ "$name" == "10" ]; then echo True; fi  # String comparison

Operators for Integer Comparison

OperatorDescription
-eqequal
-nenot equal
-gtgreater than
-gegreater than or equal to
-ltless than
-leless than or equal to

Operators for String Comparison

OperatorDescription
==equal
!=not equal
>greater than
<less than
-nstring is not null
-zstring is null

Single vs Double Square Brackets:

Filename Expansion Example:

if [[ -f *.csv ]]; then echo True; fi  # Checks for a file named "*.csv"
if [ -f *.csv ]; then echo True; fi  # Performs filename expansion

Loops

Loops are used in Bash to execute a series of commands multiple times. There are several types of loops, each serving different purposes.

For Loop

The for loop is used to iterate over a list of items or a range of values.

Syntax:

for var in list; do
  # commands
done

Example with a List:

for i in 1 2 3; do
  echo $i
done

Example with a Range:

for i in {1..3}; do
  echo $i
done

Example with Command Output:

for file in $(ls); do
  echo $file
done

While Loop

The while loop executes as long as a specified condition is true.

Syntax:

while [ condition ]; do
  # commands
done

Example:

i=1
while [ $i -le 3 ]; do
  echo $i
  ((i++))
done

Until Loop

The until loop is similar to the while loop but runs until a condition becomes true.

Syntax:

until [ condition ]; do
  # commands
done

Example:

i=1
until [ $i -gt 3 ]; do
  echo $i
  ((i++))
done

Loop Control: Break and Continue

for i in 1 2 3 4 5; do
  if [ $i -eq 3 ]; then
    break
  fi
  echo $i
done
for i in 1 2 3 4 5; do
  if [ $i -eq 3 ]; then
    continue
  fi
  echo $i
done

C-Style For Loop

Bash also supports a C-style syntax for for loops, which provides more control over the iteration process.

Syntax:

for (( initialisation; condition; increment )); do
  # commands
done

Example:

for (( i=1; i<=3; i++ )); do
  echo $i
done

Arrays

An array is a variable that holds an ordered list of values. The values are separated by spaces. The following example creates an array named array and assigns the values 1, 2, 3, 4, 5 to it:

array=(1 2 3 4 5) 

It is possible to create an array with specified element indices:

array=([3]='elem_a' [4]='elem_b')

To insert an elementat (e.g. 'abc') at a given index (e.g. 2) in the array, use the following syntax:

array=("${array[@]:0:2}" 'new' "${array[@]:2}")

To iterate over the elements of an array, use the following syntax:

items=('item_1' 'item_2' 'item_3' 'item_4')

for item in "${items[@]}"; do
  echo "$item"
done
# => item_1
# => item_2
# => item_3
# => item_4

It is often useful to print the elements of an array on a single line. The following code will print the elements of the array on a single line:

echo "${array[*]}"

Functions

Functions are used to group a sequence of commands into a single unit. They are used to perform repetitive tasks. Functions can be called from anywhere in the script. The following example creates a function named hello_world that prints the string Hello World to the standard output (stdout):

hello_world()
{
  echo "Hello World!"
}

To call the function, use the following syntax:

hello_world

The above function does not take any arguments and does not explicitly return a value. It is possible to pass any number of arguments to the function. It is also possible to return a value from the function, but only an integer from range [0,255] is allowed.

Here is a complete example of a script that defines and uses a function to sum two numbers:

#!/usr/bin/env bash

sum_two() 
{
    return $(($1 + $2))
}

sum_two 5 3
echo $?

Pipes

The pipe is used to pass the output of one command as input to the next:

ps -x | grep chromium

Redirections

But what if you'd want to save the results to a file? Bash has a redirect operator > that may be used to control where the output is delivered.

some_command > out.log            # Redirect stdout to out.log
some_command 2> err.log           # Redirect stderr to file err.log
some_command 2>&1                 # Redirect stderr to stdout
some_command 1>/dev/null 2>&1     # Silence both stdout and stderr

Complete summary:

SyntaxStdOut visibilityStdErr visibilityStdOut in fileStdErr in fileexisting file
>noyesyesnooverwrite
>>noyesyesnoappend
2>yesnonoyesoverwrite
2>>yesnonoyesappend
&>nonoyesyesoverwrite
&>>nonoyesyesappend
teeyesyesyesnooverwrite
tee -ayesyesyesnoappend
n.e. (*)yesyesnoyesoverwrite
n.e. (*)yesyesnoyesappend
|& teeyesyesyesyesoverwrite
|& tee -ayesyesyesyesappend

Formatting and linting

It is important to keep the formatting of your script as consistent as possible. <a href="https://github.com/lovesegfault/beautysh">Beautysh</a> is an amazing tool that helps you to format your script. To use it, just run the following command in a directory where your scripts are located:

beautysh **/*.sh

Additionally we advise to use <a href="https://github.com/koalaman/shellcheck">shellcheck</a> for code inspection.

shellcheck **/*.sh

Available scripts

Intro

#DescriptionCode
1Prints "Hello, world!" to the console.hello_world.sh
2Demonstrates the use of if statements to check conditions.conditionals.sh
3Shows the use of a while loop to repeatedly execute code.while_loop.sh
4Demonstrates the use of a for loop to iterate over elements.for_loop.sh
5Displays the digits of a given number, one digit per line.digits.sh
6Prints all of the numbers within a specified range, one number per line.numbers_in_interval.sh
7Prints a Christmas tree pattern to the console.christmas_tree.sh
8Prompts the user for a response to a given question and stores their response in a variable.prompt_for_answer.sh

Math

#DescriptionCode
1Performs basic arithmetic operations (addition, subtraction, multiplication, and division) on two numbers.arithmetic_operations.sh
2Calculates the sum of all the arguments passed to it, treating them as numbers.sum_args.sh
3Converts a number from the decimal (base 10) system to its equivalent in the binary (base 2) system.decimal_binary.sh
4Calculates the factorial of a given integer.factorial.sh
5Determines whether a given number is a prime number or not.is_prime.sh
6Calculates the square root of a given number.sqrt.sh

Strings

#DescriptionCode
1Counts the number of times a specific character appears in a given string.count_char.sh
2Converts all uppercase letters in a given string to lowercase.lower.sh
3Converts all lowercase letters in a given string to uppercase.upper.sh
4Checks if a given string is a palindrome, i.e., a word that is spelled the same way forwards and backwards.is_palindrome.sh
5Checks if two given strings are anagrams, i.e., if they are made up of the same letters rearranged in a different order.are_anagrams.sh
6Calculates the Hamming Distance between two strings, i.e., the number of positions at which the corresponding characters are different.hamming_distance.sh
7Sorts a given string alphabetically, considering all letters to be lowercase.sort_string.sh
8Creates a word histogram.word_histogram.sh

Arrays

#DescriptionCode
1Calculates the arithmetic mean of a given list of numbers.arith_mean.sh
2Finds the maximum value in a given array of numbers.max_array.sh
3Finds the minimum value in a given array of numbers.min_array.sh
4Removes duplicates from a given array of numbers.remove_duplicates_in_array.sh

Files

#DescriptionCode
1Counts the number of files in a specified directory.count_files.sh
2Creates a new directory with a specified name.make_dir.sh
3Counts the number of lines in a specified text file.line_counter.sh
4Gets the middle line from a specified text file.middle_line.sh
5Removes duplicate lines from a specified file.remove_duplicate_lines.sh
6Replaces all forward slashes with backward slashes and vice versa in a specified file.switch_slashes.sh
7Adds specified text to the beginning of a specified file.prepend_text_to_file.sh
8Removes all lines in a specified file that contain only whitespaces.remove_empty_lines.sh
9Renames all files in a specified directory with a particular extension to a new extension.rename_extension.sh
10Strips digits from every string found in a given file.strip_digits.sh
11Lists the most recently modified files in a given directory.recently_modified_files.sh

System administration

#DescriptionCode
1Retrieves basic system information, such as hostname and kernel version.system_info.sh
2Determines the type and version of the operating system running on the machine.check_os.sh
3Checks whether the current user has root privileges.check_if_root.sh
4Checks if the apt command, used for package management on Debian-based systems, is available on the machine.check_apt_avail.sh
5Retrieves the size of the machine's random access memory (RAM).ram_memory.sh
6Gets the current temperature of the machine's central processing unit (CPU).cpu_temp.sh
7Retrieves the current overall CPU usage of the machine.cpu_usage.sh
8Blocks certain websites from being visited on the local machine by modifying the hosts file.web_block.sh
9Creates a backup of the system's files, compresses the backup, and encrypts the resulting archive for storage.backup.sh
10Displays processes that are not being waited on by any parent process. Orphan processes are created when the parent process terminates.orphans.sh
11Displays processes that are in an undead state, also known as a "zombie" state. Zombie processes have completed execution but remain in the process table.zombies.sh

Programming workflow

#DescriptionCode
1Removes the carriage return character (\r) from the given files, which may be present in files transferred between systems with different line ending conventions.remove_carriage_return.sh
2Replaces all characters with diacritical marks in the given files with their non-diacritical counterparts. Diacritical marks are small signs added above or below letters to indicate different pronunciations or tones in some languages.remove_diacritics.sh
3Changes all spaces in file names to underscores and converts them to lowercase. This can be useful for making the file names more compatible with systems that do not support spaces in file names or for making the file names easier to read or type.correct_file_names.sh
4Removes any trailing whitespace characters (spaces or tabs) from the end of every file in a given directory. Trailing whitespace can cause formatting issues or interfere with certain tools and processes.remove_trailing_whitespaces.sh
5Formats and beautifies every shell script found in the current repository. This can make the scripts easier to read and maintain by adding consistent indentation and whitespace.beautify_script.sh
6Finds functions and classes in a Python project that are not being used or called anywhere in the code. This can help identify and remove unnecessary code, which can improve the project's performance and maintainability.dead_code.sh

Git

#DescriptionCode
1Resets the local repository to match the state of the remote repository, discarding any local commits and changes. This can be useful for starting over or synchronizing with the latest version on the remote repository.reset_to_origin.sh
2Deletes the specified branch both locally and on the remote repository. This can be useful for removing branches that are no longer needed or for consolidating multiple branches into a single branch.remove_branch.sh
3Counts the total number of lines of code in a git repository, including lines in all branches and commits. This can be useful for tracking the size and complexity of a project over time.count_lines_of_code.sh
4Combines multiple commits into a single commit. This can be useful for simplifying a commit history or for cleaning up a series of small, incremental commits that were made in error.squash_n_last_commits.sh
5Removes the n last commits from the repository. This can be useful for undoing mistakes or for removing sensitive information that was accidentally committed.remove_n_last_commits.sh
6Changes the date of the last commit in the repository. This can be useful for altering the commit history for cosmetic purposes.change_commit_date.sh
7Downloads all of the public repositories belonging to a specified user on GitHub. This can be useful for backing up repositories.download_all_github_repos.sh
8Squashes all commits on a specified Git branch into a single commit.squash_branch.sh
9Counts the total lines changed by a specific author in a Git repository.contributions_by_git_author.sh

Utility

#DescriptionCode
1Finds the public IP address of the device running the script.ip_info.sh
2Deletes all files in the trash bin.empty_trash.sh
3Extracts files with a specified extension from a given directory.extract.sh
4Determines which programs are currently using a specified port number on the local system.program_on_port.sh
5Converts month names to numbers and vice versa in a string. For example, "January" to "1" and "1" to "January".month_to_number.sh
6Creates command aliases for all the scripts in a specified directory, allowing them to be run by simply typing their names.alias_all_the_scripts.sh
7Generates a random integer within a given range. The range can be specified as arguments to the script.rand_int.sh
8Generates a random password of the specified length, using a combination of letters, numbers, and special characters.random_password.sh
9Measures the time it takes to run a program with the specified input parameters. Output the elapsed time in seconds.time_execution.sh
10Downloads the audio from a YouTube video or playlist in MP3 format. Specify the video or playlist URL and the destination directory for the downloaded files.youtube_to_mp3.sh
11Clears the local caches in the user's cache directory (e.g. ~/.cache) that are older than a specified number of days.clear_cache.sh
12Resizes all JPG files in the current directory to a specified dimension (A4).resize_to_a4

References

Official Documentation

Guides and Tutorials

Learning Platforms

Community and Support

Tools and Utilities

Books

Blogs and Articles

How to Contribute

We encourage contributions that enhance the repository's value. To contribute:

  1. Fork the repository.
  2. Create your feature branch (git checkout -b feature/AmazingFeature).
  3. Commit your changes (git commit -m 'Add some AmazingFeature').
  4. Push to the branch (git push origin feature/AmazingFeature).
  5. Open a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Star History

Star History Chart