Linux Bash

Providing immersive and explanatory content in a simple way anybody can understand.

  • Posted on
    Featured Image
    Introduction to GPIO Control in Linux General Purpose Input/Output (GPIO) pins are versatile interfaces found in various microprocessors and microcontroller boards. They allow interaction with different electronic components like LEDs, sensors, and switches. Linux, with its vast capabilities and broad device support, offers a unique method for interacting with GPIO pins called sysfs. This approach will be our focus today as we delve into how you can manipulate these pins directly from a Linux Bash shell. A: sysfs is a virtual filesystem in Linux that provides a tree-like hierarchy of device information, allowing user space processes to interact with kernel objects.
  • Posted on
    Featured Image
    One of the most critical aspects of managing a Linux system, especially in environments involving physical servers or high-performance computing, is monitoring the CPU temperature. High temperatures can lead to reduced performance, hardware malfunctions, and system crashes. Thankfully, Linux provides a way to track the temperature through the file system. In this article, we'll explore how to read CPU temperature details via the /sys/class/thermal/thermal_zone*/temp directory. Q&A: Reading CPU Temperature in Linux A1: In Linux, the /sys/class/thermal/thermal_zone*/temp directory is part of the sysfs (System Filesystem) that exposes information and interfaces provided by the kernel.
  • Posted on
    Featured Image
    Debugging in Bash can sometimes feel like navigating a dark labyrinth. Fortunately, Bash provides powerful built-in mechanisms to illuminate your scripts' execution paths. One such capability involves utilizing set -x alongside the lesser-known BASH_XTRACEFD. Let’s explore how to leverage these features effectively through a question and answer format. A1: The set -x command is a debug tool in Bash scripts that prints each command to the terminal as it's executed, along with its expanded arguments. This feature is commonly used to trace what happens during the execution of a script.
  • Posted on
    Featured Image
    In the world of software development, particularly in systems programming, understanding how an application behaves under erroneous conditions, such as memory access violations, is crucial. This typically involves exploring how your program responds to various signals such as a segmentation fault (SIGSEGV). In this article, we will explore how to intentionally trigger a segmentation fault in a Bash script to test signal handling mechanisms. Q: What is a segmentation fault? A: A segmentation fault (often abbreviated as segfault) is a specific kind of error caused by accessing memory that “does not belong” to you. It's a way your program tells you that you are attempting to access memory in a way that is not allowed.
  • Posted on
    Featured Image
    In this blog post, we'll delve into the use of the FUNCNAME array in Bash, an incredibly useful tool for accessing the call stack of functions. By the end of this article, you'll understand how to utilize FUNCNAME to debug and manage function call hierarchies effectively. Q1: What is the FUNCNAME array in Bash? A1: FUNCNAME is a Bash shell array that holds the names of all functions currently in the execution call stack. The element at index 0 is the name of the currently executing function, with the rest of the array containing the names of the functions that had invoked this function, thus showing the entire call hierarchy.
  • Posted on
    Featured Image
    Bash's autocomplete feature is a powerful tool that enhances the command-line experience by reducing typing and minimizing mistakes. Equipped with the compopt builtin, Bash allows for more dynamic control over these autocomplete behaviors. In this blog, we delve into how to utilize compopt effectively, illustrated with clear examples and a demonstration script. Q: What is compopt and how does it relate to Bash autocomplete? A: compopt is a builtin command in Bash that allows you to modify completion options dynamically for programmable completions. It is used to fine-tune the behavior of the autocomplete feature, deciding how and what gets completed.
  • Posted on
    Featured Image
    Q1: What exactly is an arithmetic expression in Bash? An arithmetic expression in Bash allows you to perform calculations and manipulate numeric values. Expressions like 1 + 2 or a * b are evaluated using Bash's arithmetic context, which you can invoke using double parentheses, $(( expression )). Q2: What are side effects in the context of arithmetic expressions? In programming, side effects refer to changes that an operation makes apart from returning a value, which may affect the state elsewhere in the system or script. In Bash arithmetic, side effects are most commonly seen with the increment ++ and decrement -- operators. They modify the value of a variable and, at the same time, use the new or old value in an expression.
  • Posted on
    Featured Image
    In the realm of Linux computing and Bash scripting, understanding how memory allocation works can significantly enhance script performance and predictability. Bash typically uses mmap() to allocate large blocks of memory but can be forced to use malloc() instead. Let’s explore why you might want to do this, how to achieve it, and the practical implications through a question-answer format. Q&A on Forcing malloc in Bash Q1: What are malloc and mmap, and how do they relate to Bash? A1: In context of memory management in Unix-like systems, malloc() is a standard C library function that allocates memory from the heap and mmap() maps files or devices into memory.
  • Posted on
    Featured Image
    Q1: What is shopt -s extdebug in a Linux Bash environment? A1: shopt -s extdebug is a Bash built-in command that enables extended debugging features. When you set this option with shopt -s extdebug, several debugging and verbose functionalities are enabled in the shell, such as enhanced function tracing and more detailed error reports. This is particularly useful for script developers looking to debug complex scripts. Q2: How does declare -ft work in conjunction with shopt -s extdebug? A2: The declare -ft command is used specifically to trace shell functions. When used by itself, declare -f lists functions and their definitions.
  • Posted on
    Featured Image
    Welcome to the world of Linux Bash Command customization! Today, we will delve into an intriguing technique that many Linux users might find handy, especially those who manage numerous applications, different tool versions, or systems with tight security requirements. We will explore how to override the PATH lookup for a command using env -i /absolute/path/to/bin. Q: What does it mean to "override the PATH lookup" for a command? A: In Linux and UNIX-like systems, the PATH is an environmental variable that tells the shell which directories to search for executable files in response to commands issued by a user.
  • Posted on
    Featured Image
    When working in Linux Bash, aliases are a powerful tool that simplifies lengthy commands into shorter, more manageable aliases. However, there are times when the original functionality of a command is needed, bypassing any aliases that have been set. This blog explores how to disable alias expansion for a single command using command or \. Alias expansion in Linux Bash occurs when aliases replace certain command names with others, typically replacing a complex command or one with long options with a simpler, shorter version for ease of use.
  • Posted on
    Featured Image
    In the world of Linux scripting with Bash, understanding how your scripts operate and handling unexpected behaviors efficiently can be drastically improved by advanced Bash features such as trap and environment variables like BASH_COMMAND. In this blog, we explore how to leverage these to log the exact command being executed, thereby improving debugging and script robustness. A: trap is a command used in Bash to specify a script or command to execute when the shell receives a specific signal or when other predefined events occur in the script. Events could be signals such as SIGINT (triggered by Ctrl+C), SIGTERM, or even script-defined events like EXIT.
  • Posted on
    Featured Image
    Q: What is ionice and why is it important? A: ionice is a command in Linux that allows the user to set the I/O scheduling priority of a program. I/O, or Input/Output operations, involve data transfer between the computer’s persistent storage (like HDDs or SSDs) and RAM. In scenarios where multiple applications are competing for disk access, controlling the priority of these operations can significantly influence the performance of the tasks being executed. Q: How does ionice work, and how can you use it? A: The ionice command works by assigning an I/O class and priority to a process. This determines how the process will be treated by the kernel with respect to disk I/O.
  • Posted on
    Featured Image
    Creating a RAM disk in a Linux system might sound like a task reserved for experts, but it’s quite straightforward once you understand the basics. This digital endeavor can significantly speed up the operations that require frequent disk reads/writes by leveraging the high-speed nature of RAM. In this blog post, we’ll discuss how to setup a RAM disk using tmpfs, a common temporary file storage filesystem in Linux. Q: What is a RAM disk? A: A RAM disk is a portion of RAM that your system uses as if it were a disk drive. It is much faster than using a physical disk because RAM has a higher speed of data access. The data stored in a RAM disk is temporary as it gets erased when the system shuts down or reboots.
  • Posted on
    Featured Image
    Q1: What is ebtables and why is it used to filter ARP traffic? A1: ebtables is a utility for filtering traffic passing through a Linux-based bridge. It operates at the Ethernet layer, making it perfectly suitable for handling ARP (Address Resolution Protocol) packets, which occur at the link layer. ebtables is commonly used to enforce MAC-layer filters, manage network segmentation, and mitigate various types of network abuses such as ARP spoofing. Q2: What are the typical commands used in ebtables for filtering ARP requests and replies? A2: The basic commands used in ebtables to filter ARP requests (ARP REQUEST) and replies (ARP REPLY) include: Adding a rule: ebtables -A Specifying the chain: e.g.
  • Posted on
    Featured Image
    System administrators and developers often need to monitor the memory usage of individual processes to ensure optimal performance and prevent system resources from being over-utilized. One useful tool for Linux that can assist in this task is ps_mem.py, a Python script that reports memory usage of processes with shared memory divided appropriately. Below, we'll explore how to use this tool effectively by integrating it into routine monitoring tasks. A1: ps_mem.py is a Python script used for measuring memory usage of processes on a Linux system. It provides an accurate representation by allocating shared memory to each process uniquely, giving a clearer picture of memory usage per process. Q2: Where can I find and how do I install ps_mem.
  • Posted on
    Featured Image
    In the world of Linux, efficiency and specificity are key. One powerful tool for optimizing performance is taskset, a command that assigns a process to a specific CPU or set of CPUs, allowing you to manage the system's workload effectively. Let's delve into how you can leverage taskset -c to bind a script to a specific CPU core. A1: taskset is a command-line utility in Linux that allows you to set or retrieve the CPU affinity of a process. CPU affinity is a scheduler property that "binds" a process to a given set of CPUs on the system, enhancing performance by reducing the cache misses associated with that process.
  • Posted on
    Featured Image
    Whether you’re a developer debugging an application, or a system administrator managing server configurations, understanding the interworking of Linux shared libraries (also known as dynamic link libraries) is a crucial skill. This blog post introduces the use of the LD_DEBUG=files environment variable to trace shared library dependencies in bash scripts. We’ll explore the what, why, and how, with relevant examples to help you grasp this useful tool. A1: LD_DEBUG=files enables you to see which shared libraries are being loaded when you execute a program.
  • Posted on
    Featured Image
    In the dynamic environment of Linux systems, managing the scheduling priority of running processes is a crucial task for systems administrators and power users. Tuning the priority can help in optimizing the system by allowing critical tasks to get more CPU time. This article explores how to alter a process's scheduling priority dynamically using chrt command in Linux. A: In Linux, process scheduling is the method by which the kernel allocates CPU time to various processes. This scheduling plays a significant role in determining the responsiveness and efficiency of the system. Q: What is chrt and how is it used? A: chrt stands for "change real-time attributes".
  • Posted on
    Featured Image
    Linux kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need for rebooting the system. In this article, we'll explore how to manage these modules using modprobe and rmmod commands. Q: What is a kernel module? A: A kernel module is a program that can be loaded into or unloaded from the kernel upon demand, without necessarily rebooting the system. These modules can provide additional capabilities or functions to the base Linux kernel. Q: What is modprobe? A: modprobe is a command-line utility that allows users to load a module or set of modules into the kernel.
  • Posted on
    Featured Image
    Linux offers a vast array of capabilities and features, many of which stem from the settings and controls managed by the kernel. For system administrators and power users, the ability to tweak these kernel parameters on-the-fly can be invaluable for optimizing performance and security. Today, we delve into how to use sysctl -w to enable or disable kernel features, particularly focusing on network settings such as IPv6 and ICMP responses. Q1: What is sysctl? A1: sysctl is a tool for Linux and other Unix-like operating systems that allows querying and changing kernel runtime parameters. These parameters are found in the /proc/sys/ directory and can control everything from memory management to network settings and more.
  • Posted on
    Featured Image
    When running a specific workload on a Linux server, one of the key aspects you might want to optimize is memory usage. In Linux, the vm.swappiness parameter controls the degree to which the system favors swapping memory pages out of physical memory to increase the amount of free memory available. Adjusting this setting can significantly affect system performance, especially in a resource-intensive environment. Q&A: Tuning vm.swappiness for Specific Workloads A1: vm.swappiness is a Linux kernel parameter that controls how much the kernel prefers swapping to keeping processes in physical memory. It's a scale from 0 to 100, where a lower value means less swapping is done, and a higher value means more aggressive swapping.
  • Posted on
    Featured Image
    Q1: What is nmap and its Scripting Engine (NSE)? A1: nmap (Network Mapper) is a powerful network discovery and security auditing tool widely used in the cybersecurity field. NSE (Nmap Scripting Engine) is a feature of nmap that allows users to write specific scripts to automate a wide range of networking tasks. These scripts can perform network checks, detect vulnerabilities, and gather network information automatically. Q2: How can NSE scripts be utilized in a Bash script? A2: Bash scripting can be utilized to automate the running of nmap and its scripts on multiple targets or different networks thereby enhancing productivity and effectiveness. By integrating NSE scripts into Bash, complex tasks can be reduced to simple, reusable scripts.
  • Posted on
    Featured Image
    In the realm of web development and system administration, there are often needs to fetch or send data from and to various servers. This task may become challenging when dealing with proxy servers. Fortunately, curl, a powerful tool, offers a method to bypass proxy settings for specific commands without altering environment variables. Let’s dive into how you can achieve this using the --socks5-hostname option. A1: curl is a command-line tool used for transferring data with URL syntax. It supports various protocols including HTTP, HTTPS, FTP, FTPS, SCP, SFTP, and more. It is known for its versatility and widespread usage in handling data across networks.
  • Posted on
    Featured Image
    File security is a fundamental concern for individuals and businesses alike. As data breaches become more frequent, encrypting sensitive information becomes critical. One of the tools available for Linux users is OpenSSL, a powerful toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. In this blog, we explore how to symmetrically encrypt and decrypt files using OpenSSL with the aes-256-cbc cipher and the pbkdf2 option. Q1: What is symmetric encryption? A1: Symmetric encryption is a type of encryption where the same key is used for both encryption and decryption. It's effective for securing data as long as the key remains private and shared only among authorized users.