{"id":19327811,"url":"https://github.com/ekedonald/shell-scripting","last_synced_at":"2026-06-08T23:31:20.057Z","repository":{"id":199154193,"uuid":"701341903","full_name":"ekedonald/Shell-Scripting","owner":"ekedonald","description":"This project shows how to create a Shell Script to perform operations such as: Directory Manipulation and Navigation, File Operations and Sorting, Math Calculations, File Backup and Time-stamping.","archived":false,"fork":false,"pushed_at":"2023-10-10T09:32:28.000Z","size":1733,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-16T23:22:55.868Z","etag":null,"topics":["linux","shell-scripting"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ekedonald.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-06T12:46:08.000Z","updated_at":"2023-11-20T19:03:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"c3cc74dd-fc9e-421e-a65c-cbf4d73fe4a0","html_url":"https://github.com/ekedonald/Shell-Scripting","commit_stats":null,"previous_names":["ekedonald/shell-scripting"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ekedonald/Shell-Scripting","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekedonald%2FShell-Scripting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekedonald%2FShell-Scripting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekedonald%2FShell-Scripting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekedonald%2FShell-Scripting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ekedonald","download_url":"https://codeload.github.com/ekedonald/Shell-Scripting/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekedonald%2FShell-Scripting/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34085321,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-08T02:00:07.615Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["linux","shell-scripting"],"created_at":"2024-11-10T02:18:51.240Z","updated_at":"2026-06-08T23:31:20.038Z","avatar_url":"https://github.com/ekedonald.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Shell Scripting\n___\nShell scripting is a way of automating and scripting tasks on a Unix or Unix-like operating system, such as Linux or macOS, using shell commands and scripting constructs. A shell script is a series of commands and instructions written in a plain text file, which can be executed by the system's shell interpreter. The shell interpreter reads and executes the commands in the script one by one, allowing you to automate repetitive tasks, perform system administration and more.\n\nFor instance, when we want to clone a git repo, we type the command `git clone` and pass in the link to the repository. In less than no time we can see the repo downloaded into our local machine. Let's say you are given a task to clone 1000 repositories. Yes, you can type the `git clone` command 1000 times. That gets the job done. Someone with not-so-great patience may be unable to complete the task.\n\nThis is where shell scripting comes in. Shell scripting helps you automate repetitive tasks. We can simply write a script that does the job of cloning the 1000 repositories. We call it once and the job is done. We have the advantage of using it again whenever we are assigned the same task.\n\nNote that for you to run a script, you have the give the file executable permissions using the command shown below:\n\n```bash\nsudo chmod +x \u003cfilename\u003e\n```\n\n## Shell Scripting Syntax Elements\n## 1. Variables\nBash allows you to define and work with variables. Variables can store data of various types such as numbers, strings and arrays. You can assign values to variables using the `=` operator and access their values using the variable name preceded by a `$` sign. \n\n### Assigning a value to a variable.\n\n```bash\nname=\"John\"\n```\n![name=John](./images/1.%20name=John.png)\n\n### Retrieving the value from a variable.\n\n```bash\necho $name\n```\n\n![echo name](./images/1.%20echo%20name.png)\n\n## 2. Control Flow\nBash provides control flow statements like if-else, for loops, while loops and case statements to control the flow of execution in your scripts. These statements allow you to make decisions, iterate over lists and execute different commands based on conditions.\n\n### Using if-else to execute a script.\n\n* Create a file named `control_flow.sh` using the command shown below:\n\n```bash\ntouch control_flow.sh\n```\n\n![touch control flow](./images/1.%20touch%20control%20flow.png)\n\n* Give the file executable permissions in order to run the script using the command shown below:\n\n```bash\nsudo chmod +x control_flow.sh\n```\n\n![chmod control flow](./images/2.%20chmod%20control%20flow.png)\n\n* Use the command shown below to open the file for you to input data.\n\n```bash\nvi control_flow.sh\n```\n\n![control flow](./images/1.%20vi%20control%20flow.png)\n\n* Copy and paste the code shown below into the file:\n\n```bash\n#!/bin/bash\n\n# Example script to check if a number is positive, negative, or zero\n\nread -p \"Enter a number: \" num\n\nif [ $num -gt 0 ]; then\n    echo \"The number is positive.\"\nelif [ $num -lt 0 ]; then\n    echo \"The number is negative.\"\nelse\n    echo \"The number is zero.\"\nfi\n```\n\n![control flow script](./images/2.%20control%20flow%20script.png)\n\n* Save and quit the file by using the `esc` key and typing `:wq!`\n\n*This piece of code prompts you to type a number and prints a statement stating whether the number is positive or negative.*\n\n* Run the script and enter a positive integer `2` in the prompt.\n\n![control flow positive](./images/2.%20:control%20flow%20positive.png)\n\n* Run the script and enter a negative integer `-2` in the prompt.\n\n![control flow negative](./images/2.%20:control%20flow%20negative.png)\n\n* Run the script and enter `0` in the prompt.\n\n![control flow zero](./images/2.%20:control%20flow%20negative.png)\n\n\n\n### Iterating through a list using a for loop.\n\n* Create a file named `test2.sh` using the command shown below:\n\n```bash\ntouch test2.sh\n```\n\n![touch test2](./images/2.%20touch%20test2.png)\n\n* Give the file executable permissions by using the command shown below:\n\n```bash\nsudo chmod +x test2.sh\n```\n\n![chmod test2](./images/2.%20chmod%20test2.png)\n\n* Run the following command in order to open the script:\n\n```bash\nvi test2.sh\n```\n* Copy and paste the code shown below:\n\n```bash\n#!/bin/bash\n\n# Example script to print numbers from 1 to 5 using a for loop\n\nfor (( i=1; i\u003c=5; i++ ))\ndo\n    echo $i\ndone\n```\n![test2 script](./images/2.%20test2%20script.png)\n\nThe result is shown below:\n\n![test 2](./images/2.%20:test2.png)\n\n## 3. Command Substitution\n Command substitution allows you to capture the output of a command and use it as a value within your script. You can use the backtick or the $()syntax for command substitution.\n\n### Using backtick for command substitution\n\n* Run the following command to declare the variable for *current_date*:\n\n```bash\ncurrent_date=`date +%Y-%m-%d`\n```\n\n![current_date1](./images/3.%20current_date%20ydm.png)\n\n* Run the following command to display the variable stored:\n\n```bash\necho $current_date\n```\n\n![echo current_date1](./images/3.%20echo%20current_date1.png)\n\n### Using `%()` syntax for command substitution\n\n* Run the following command to declare the variable for *current_date*:\n\n```bash\ncurrent_date=$(date +%Y-%d-%m)\n```\n![current_date2](./images/3.%20current_date%20ymd.png)\n\n* Run the following command to display the new variable stored.\n\n```bash\necho $current_date\n```\n\n![echo current_date2](./images/3.%20echo%20current_date2.png)\n\n\n## 4. Input and Output\nBash provides various ways to handle input and output. You can use the read command to accept user input and output text to the console using the echo command. \n\nAdditionally, you can redirect input and output using operators like `\u003e` *(output to a file)*, `\u003c` *(input from a file)* and `|` *(pipe the output of one command as input to another)*.\n\n* Accept user input.\n\n```bash\necho \"Enter your name:\"\nread name\n```\n\n![echo read name](./images/4.%20echo%20read%20name.png)\n\n* Output text to the terminal.\n\n```bash\necho \"Hello World\"\n```\n\n![echo Hello World](./images/4.%20echo%20hello%20world.png)\n\n* Output the result of a command into a file.\n\n```bash\necho \"hello world\" \u003e index.txt\n```\n\n![echo hello world \u003e index.txt](./images/4.%20echo%20hello%20world%20\u003e%20index_txt.png)\n\n* Pass the content of a file as input to a command.\n\n```bash\ngrep \"hello\" \u003c index.txt\n```\n\n![grep hello \u003c index.txt](./images/4.%20grep%20hello%20\u003c%20index_txt.png)\n\n* Pass the result of a command as input to another command.\n\n```bash\necho \"hello world\" | grep \"world\"\n```\n\n![echo hello world | grep world](./images/4.%20echo%20hello%20world%20grep%20world.png)\n\n## 5. Functions\nBash allows you to define and use functions to group related commands together. Functions provide a way to modularize your code and make it more reusable. You can define functions using the function keyword or simply by declaring the function name followed by parentheses.\n\n* Create a file named `function.sh` using the following command:\n\n```bash\ntouch function.sh\n```\n\n![touch function](./images/5.%20touch%20function.png)\n\n* Give the file executable permissions using the following command:\n\n```bash\nsudo chmod +x function.sh\n```\n\n![chmod function](./images/5.%20chmod%20function.png)\n\n* Run the following command to open the file:\n\n```bash\nvi function.sh\n```\n* Copy and paste the code below into the file:\n\n```bash\n#!/bin/bash\n\n# Define a function to greet the user\ngreet() {\n    echo \"Hello, $1! Nice to meet you.\"\n}\n\n# Call the greet function and pass the name as an argument\ngreet \"John\"\n```\n\n![function script](./images/5.%20function%20script.png)\n\n* Run the script using the command shown below:\n\n```bash\n./ function.sh\n```\n\n![function.sh](./images/5.%20:function.png)\n\n## Writing Your First Shell Script\n\n### Step 1 \nOn your terminal, create and open a folder named `shell-scripting` using the commands shown below:\n\n```bash\nmkdir shell-scripting\n```\n\n![mkdir shell-scripting](./images/6.%20mkdir%20shell%20scripting.png)\n\n```bash\ncd shell-scripting\n```\n\n![cd shell-scripting](./images/6.%20cd%20shell%20scripting.png)\n\nThis will hold all the script we will write.\n\n### Step 2\nCreate a file named `user-input.sh` using the command shown below:\n\n```bash\ntouch user-input.sh\n```\n\n![touch user-input](./images/6.%20touch%20user-input.png)\n\n### Step 3\nUse the `vi user-input.sh` command to open the file then copy and paste the block of code below:\n\n```bash\n#!/bin/bash\n\n# Prompt the user for their name\necho \"Enter your name:\"\nread name\n\n# Display a greeting with the entered name\necho \"Hello, $name! Nice to meet you.\"\n```\n\n![user-input script](./images/6.%20user-input%20script.png)\n\n**Summary of the code block**: The script prompts for your name. When you type your name, it displays the text *hello! Nice to meet you*. Also `#!/bin/bash` helps you specify the type of bash interpreter used to execute the script.\n\n### Step 4\nSave the file using the `esc` key and `:wq!`.\n\n### Step 5\nRun the command below to make the file executable:\n\n```bash\nsudo chmod +x user-input.sh\n```\n\n![chmod user-input](./images/6.%20chmod%20user-input.png)\n\n### Step 6\nRun the script using the command shown below:\n\n ```bash\n ./user-input.sh\n ```\n\n ![user-input.sh](./images/6.%20:user-input.png)\n\n## Directory Manipulation and Navigation\nWe will be writing a simple shell script as a way of practicing what we learned. This script will display the current directory, create a new directory called \"*my_directory*\", change to that directory, create two files inside it, list the files, move back one level up, remove the \"*my_directory*\" and its contents and finally list the files in the current directory again.\n\nProceed by following the steps below:\n\n### Step 1\nCreate a file named `navigating-linux-filesystem.sh` using the following command:\n\n```bash\nvi navigating-linux-filesystem.sh\n```\n\n### Step 2\nPaste the code block below into the file:\n\n```bash\n#!/bin/bash\n\n# Display current directory\necho \"Current directory: $PWD\"\n\n# Create a new directory\necho \"Creating a new directory...\"\nmkdir my_directory\necho \"New directory created.\"\n\n# Change to the new directory\necho \"Changing to the new directory...\"\ncd my_directory\necho \"Current directory: $PWD\"\n\n# Create some files\necho \"Creating files...\"\ntouch file1.txt\ntouch file2.txt\necho \"Files created.\"\n\n# List the files in the current directory\necho \"Files in the current directory:\"\nls\n\n# Move one level up\necho \"Moving one level up...\"\ncd ..\necho \"Current directory: $PWD\"\n\n# Remove the new directory and its contents\necho \"Removing the new directory...\"\nrm -rf my_directory\necho \"Directory removed.\"\n\n# List the files in the current directory again\necho \"Files in the current directory:\"\nls\n```\n\n![navigating linux filesystem script](./images/7.%20navigating%20linux%20filesystem%20script.png)\n\n### Step 3\nRun the command below to give executable permission on the file:\n\n```bash\nsudo chmod +x navigating-linux-filesystem.sh\n```\n\n![chmod navigating-linux-filesystem](./images/7.%20chmod%20navigating%20linux%20filesystem.png)\n\n### Step 4\nRun your script using the command shown below:\n\n ```bash\n./navigating-linux-filesystem.sh\n```\n\n![navigating-linux-filesystem.sh](./images/7.%20:navigating%20linux%20filesystem.png)\n\n## File Operations and Sorting\nHere, we will be writing a simple shell that focuses on *File Operations and Sorting*. This script creates three files *(file1.txt, file2.txt and file3.txt)*, displays the files in their current order, sorts them alphabetically, saves the sorted files.txt, displays the sorted files, removes the original files, renames the *sorted_files_sorted_alphabetically.txt* and finally displays the contents of the final sorted file.\n\nLet's proceed using the steps below:\n\n### Step 1\nOpen your terminal and create a file named `sorting.sh` using the command shown below:\n\n```bash\nvi sorting.sh\n```\n\n### Step 2\nCopy and paste the code block below into the file:\n\n```bash\n#!/bin/bash\n\n# Create three files\necho \"Creating files...\"\necho \"This is file3.\" \u003e file3.txt\necho \"This is file1.\" \u003e file1.txt\necho \"This is file2.\" \u003e file2.txt\necho \"Files created.\"\n\n# Display the files in their current order\necho \"Files in their current order:\"\nls\n\n# Sort the files alphabetically\necho \"Sorting files alphabetically...\"\nls | sort \u003e sorted_files.txt\necho \"Files sorted.\"\n\n# Display the sorted files\necho \"Sorted files:\"\ncat sorted_files.txt\n\n# Remove the original files\necho \"Removing original files...\"\nrm file1.txt file2.txt file3.txt\necho \"Original files removed.\"\n\n# Rename the sorted file to a more descriptive name\necho \"Renaming sorted file...\"\nmv sorted_files.txt sorted_files_sorted_alphabetically.txt\necho \"File renamed.\"\n\n# Display the final sorted file\necho \"Final sorted file:\"\ncat sorted_files_sorted_alphabetically.txt\n```\n\n![sorting script](./images/8.%20sorting%20script.png)\n\n### Step 3\nSet execute permission on *sorting.sh* using the command shown below:\n\n```bash\nsudo chmod +x sorting.sh\n```\n\n![chmod sorting](./images/8.%20chmod%20sorting.png)\n\n### Step 4\nRun the script using the command shown below:\n\n```bash\n./sorting.sh\n```\n![sorting](./images/8.%20:sorting.png)\n\n## Working With Numbers and Calculations\nThis script defines two variables *num1* and *num2* with numeric values, performs basic arithmetic operations *(addition, subtraction, multiplication, division and modulus)* and displays the results. It also performs more complex calculations such as raising *num1* to the power of 2 and calculating the square root of *num2* and displays those results as well.\n\nLet's proceed by following the steps below:\n\n### Step 1\nOn your terminal, create a file named `calculations.sh` using the command below:\n\n```bash\nvi calculations.sh\n```\n\n### Step 2\nCopy and paste the code block below into the file:\n\n```bash\n#!/bin/bash\n\n# Define two variables with numeric values\nnum1=10\nnum2=5\n\n# Perform basic arithmetic operations\nsum=$((num1 + num2))\ndifference=$((num1 - num2))\nproduct=$((num1 * num2))\nquotient=$((num1 / num2))\nremainder=$((num1 % num2))\n\n# Display the results\necho \"Number 1: $num1\"\necho \"Number 2: $num2\"\necho \"Sum: $sum\"\necho \"Difference: $difference\"\necho \"Product: $product\"\necho \"Quotient: $quotient\"\necho \"Remainder: $remainder\"\n\n# Perform some more complex calculations\npower_of_2=$((num1 ** 2))\nsquare_root=$(awk \"BEGIN {print sqrt($num2)}\")\n\n# Display the results\necho \"Number 1 raised to the power of 2: $power_of_2\"\necho \"Square root of number 2: $square_root\"\n```\n\n![claculations script](./images/9.%20calculations%20script.png)\n\n### Step 3\nSet execute permission on *calculations.sh* using the command shown below:\n\n```bash\nsudo chmod +x calculations.sh\n```\n\n![chmod calculations](./images/9.%20chmod%20calculations.png)\n\n### Step 4\nRun the script using the command shown below:\n\n```bash\n./calculations.sh\n```\n\n![calculations](./images/9.%20:calculations.png)\n\n## File Backup and Timestamping\nAs a DevOps Engineer backing up databases and other storage devices is one of the most common tasks you can carry out.\n\nThis script defines the source directory paths. It then creates a timestamp using the current date and time, and creates a backup directory using the `cp` command with the `-r` option for recursive copying. \n\nFinally, it displays a message indicating the completion of the backup process and shows the path of the backup directory with the timestamp.\n\nProceed by following the steps below:\n\n### Step 1\nOn your terminal, create a file named `backup.sh` using the command shown below:\n\n```bash\nvi backup.sh\n```\n\n### Step 2\nCopy and paste the code below into the file:\n\n```bash\n#!/bin/bash\n\n# Define the source directory and backup directory\n\n# Define the source directory using your preferred directory\n#   source_dir=\"/path/to/source_directory\"\nsource_dir=\"/home/vagrant/shell-scripting/test\"\n\n# Define the backup directory using your preferred directory\n#   backup_dir=\"/path/to/backup_directory\"\nbackup_dir=\"/home/vagrant/shell-scripting/backup\"\n\n# Create a timestamp with the current date and time\ntimestamp=$(date +\"%Y%m%d%H%M%S\")\n\n# Create a backup directory with the timestamp\nbackup_dir_with_timestamp=\"$backup_dir/backup_$timestamp\"\n\n# Create the source directory\nmkdir -p \"$source_dir\"\n\n# Create the backup directory\nmkdir -p \"$backup_dir_with_timestamp\"\n\n# Copy all files from the source directory to the backup directory\ncp -r \"$source_dir\" \"$backup_dir_with_timestamp\"\n\n# Display a message indicating the backup process is complete\necho \"Backup completed. Files copied to: $backup_dir_with_timestamp\"\n```\n\n![backup script](./images/10.%20backup%20script.png)\n\n### Step 3\nSet execute permission on *backup.sh* using the command shown below:\n\n```bash\nsudo chmod +x backup.sh\n```\n\n![chmod backup](./images/10.%20chmod%20backup.png)\n\n### Step 4\nRun the script using the command shown below:\n\n```bash\n./backup.sh\n```\n\n![backup.sh](./images/10.%20:backup.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekedonald%2Fshell-scripting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fekedonald%2Fshell-scripting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekedonald%2Fshell-scripting/lists"}