Active Cooling vs Passive Cooling: What’s the difference?
When you are setting up your homelab, you’ll come across the terms active and passive cooling. Learn about the difference between the two.
Read MoreWhen you are setting up your homelab, you’ll come across the terms active and passive cooling. Learn about the difference between the two.
Read MoreThe sshd splitting progresses with sshd-auth, isolating authentication in a separate binary, and reducing pre-auth attack surface in OpenSSH.
The post OpenSSH Splits Again: New Authentication Binary Unveiled appeared first on Linux Today.
Linux is a powerful operating system, not only because of its stability and open-source nature but also because of the incredible control it gives users over their system. One of the most effective ways to harness this power is through Bash scripting, which allows you to automate tasks, simplify processes, and unlock greater efficiency in managing Linux environments.
In this tutorial, we’ll walk you through the basics of creating and running a Bash script. Whether you’re new to Linux or looking to expand your skills, this guide will get you started with scripting.
Bash (short for Bourne Again Shell) is a command-line interpreter that provides a user interface for Unix-based systems, including Linux. The shell is where you can type commands, run programs, and interact with the underlying OS. Bash scripts are text files that contain a series of commands executed line by line.
By learning how to write Bash scripts, you can automate repetitive tasks, schedule jobs, and even build complex workflows.
To create a new Bash script, you first need to create a file that will store your commands. Open a terminal and use a text editor (like nano or vim) to create a new file. For this example, let’s use nano:
nano myscript.sh
The .sh extension is commonly used for Bash scripts, though it’s not required. You can name the file anything you want.
At the very top of your script file, include a shebang line. This tells the system that the script should be executed using Bash. The shebang is written as follows:
#!/bin/bash
Without this line, your script might not know which shell to use, especially if you’re running it in a different environment.
Now, let’s add some simple commands. For example, we can create a script that outputs “Hello, World!” and shows the current date:
#!/bin/bash
# This is a comment
echo “Hello, World!”
echo “Today is: $(date)”
echo command prints text to the terminal.$(date) command executes the `date` command and substitutes its output.Once you’ve written your script, save the file by pressing CTRL + O in nano, then press Enter. To exit, press CTRL + X.
Before running your script, you need to give it executable permissions. You can do this using the chmod command:
chmod +x myscript.sh
This command tells Linux that the file can be executed like a program.
Now that your script is executable, you can run it by typing:
./myscript.sh
You should see output similar to:
“Hello, World!”
“Today is: Mon Oct 16 12:34:56 UTC 2024”
Congratulations! You’ve just written and executed your first Bash script.
Bash scripts become more useful when you introduce variables and basic logic. Let’s expand our script.
You can store values in variables and use them throughout your script. Here’s an example of a script that asks for your name and greets you:
#!/bin/bash
# Prompt the user for their name
echo “Enter your name:”
read name
# Greet the user
echo “Hello, $name! Welcome to Bash scripting.”
In this script:
read command captures user input and stores it in the name variable.$).Bash supports if statements for basic decision-making. Here’s an example that checks if the user is root (the system administrator):
#!/bin/bash
# Check if the user is root
if [ “$USER” == “root” ]; then
echo “Hello, root user!”
else
echo “You are not the root user.”
fi
The if statement checks if the USER environment variable is equal to root. The then block runs if the condition is true, and the else block runs if it’s false.
Bash also supports loops, which are essential when you want to repeat a task multiple times. For example, here’s a script that prints numbers from 1 to 5:
#!/bin/bash
# Loop from 1 to 5
for i in {1..5}; do
echo “Number: $i”
done
Let’s apply what we’ve learned to automate a real task—backing up a directory. This script will copy all files from a source directory to a backup location.
#!/bin/bash
# Define variables
source_dir=“/home/user/documents”
backup_dir=“/home/user/backup”
# Create the backup directory if it doesn’t exist
mkdir -p “$backup_dir”
# Copy files
cp -r “$source_dir”/* “$backup_dir”
# Print a success message
echo “Backup completed successfully.”
To automate running this script daily, you can use cron, a task scheduler in Linux. To set this up, follow these steps:
crontab -e
0 2 * * * /path/to/myscript.sh
Save and exit. Now, your script will run automatically every day.
Bash scripting is a powerful tool that allows you to automate tasks, create workflows, and manage your Linux environment more efficiently. In this tutorial, we’ve covered the basics of writing and running Bash scripts, introduced variables, logic, and loops, and demonstrated how to automate tasks with cron.
With these fundamentals, you can now explore more advanced features like functions, error handling, and interacting with other Linux tools. Happy scripting!
Read MoreThe post How to Run Own Online SMS Portal with playSMS in Linux first appeared on Tecmint: Linux Howtos, Tutorials & Guides .playSMS is an open-source SMS management software that allows you to send and receive SMS messages using various gateways and
T…
The post How to Run Own Online SMS Portal with playSMS in Linux first appeared on Tecmint: Linux Howtos, Tutorials & Guides .playSMS is an open-source SMS management software that allows you to send and receive SMS messages using various gateways and
T…
The post CentOS Stream: The Perfect Distribution for Development Projects first appeared on Tecmint: Linux Howtos, Tutorials & Guides .CentOS Stream has become a key part of the Red Hat ecosystem, offering developers an exciting opportunity to shape th…
Read MoreA firewall is a software that acts as a protective barrier between a user’s system and external networks, allowing or blocking packets based on predefined rules. Firewalls operate mainly at the network layer, handling both IPv4 and IPv6 packets. The decision to allow or block a packet is based on rules defined in the firewall. […]
The post How to Manage Firewalld and UFW on Linux appeared first on Linux Today.
Read MoreDollar for dollar matching funds, cool swag, and you get to sleep better at night knowing that the folks at Tor are able to continue working hard to protect your privacy. What’s not to like about that?
The post FOSS Force Tor’s Raising Funds — Get Swag…
The DistroWatch news feed is brought to you by TUXEDO COMPUTERS. Rodolphe Bachelart has announced the release of Voyager Live 24.10, the latest release of the project’s Ubuntu-based desktop Linux distribution that combines GNOME and Xfce into one unif…
Read MoreThe Apt tool is an advanced package management system used in Debian-based Linux distributions, like Ubuntu, which allows you to easily install, update, and remove software packages.
Initially, APT was designed as a front-end for dpkg to work with .deb…
Ubuntu 24.10 may have only just been released, but development on the next version is getting underway and the codename for Ubuntu 25.04 revealed. Since codenames are alphabetical (as of Ubuntu 6.06 LTS; restarted at ‘A’ with 17.10) it mean…
Read MoreZRAM is a Linux kernel module that allows you to create a compressed block device in RAM, that effectively increases the amount of usable memory on your system by compressing data stored in RAM, which can be especially useful for systems with limited physical memory, allowing them to run more applications simultaneously without slowing down. […]
The post How to Install ZRAM to Boost Ubuntu Performance appeared first on Linux Today.
Read MoreDebian Developer Niko Tyni announced that Debian Unstable has been upgraded to Perl 5.40.0, bringing a range of new features, security updates, and bug fixes.
The post Debian Unstable Upgrades To Perl 5.40 appeared first on Linux Today.
Join the FSF and friends on Friday, October 18 from 12:00 to 15:00 EDT (16:00 to 19:00 UTC) to help improve the Free Software Directory.
Read MoreClonezilla Live 3.2 is here a little over three months after Clonezilla Live 3.1.3 to bump the kernel from the Linux 6.9 used in the previous version to Linux 6.11 to provide users with better hardware support. It also rebases the underlying GNU/Linux …
Read MoreA network analyzer (also known as a packet analyzer, packet sniffer, or protocol analyzer) is software that intercepts and logs traffic that passes over a computer network or part of a network. Here are 15 free packet sniffers you should check out.
The…
LibreSSL 4.0 introduces portable changes, bug fixes, and internal cleanups. Now supporting OpenBSD 7.6, improving TLS, X.509, and RSA key handling.
The post LibreSSL 4.0 Debuts with Enhanced Security and Bug Fixes appeared first on Linux Today.
Learn about the /etc/mtab file in Linux, the different sections inside it, and the similarities between mtab and fstab files with examples.
The post Understanding the /etc/mtab File in Linux appeared first on Linux Today.
This tutorial shows how to configure Ubuntu or other Linux to redirect certain URLs or domains to specific web browser, while leaving all others open in the default browser. When clicking an URL in email reader, chat app, and other apps, it by default opens the linked page in system default web browser. However, some […]
Read MoreStill powered by the Linux 6.10 kernel series, Solus 4.6 ships with a newer graphics stack, namely Mesa 24.2, for an extra boost in games and other graphics software. It also brings more usr-merge work, which aligns Solus with the compatibility efforts…
Read More