Linux bash cheat sheet

Bash cheat sheet: Top 25 commands and creating custom commands

Being well-versed in the Bourne Again SHell (Bash Shell) as a developer or Linux user can significantly boost productivity. The Bash Shell is a Swiss Army knife of great features with deceptively simple commands, and it can provide far more flexible options for organizing and streamlining your workflow.

The Bash Shell is the most widely used command-line interface (CLI) available. Bash is a Unix shell and command language that is the default login shell for the majority of Linux distributions. It provides users with a set of tools for managing tasks on a Linux system.

Linux is an open-source operating system that is widely used in the tech industry. One of the key advantages of Linux is its command-line interface, which provides users with direct access to the underlying system and allows them to execute commands and scripts using a shell such as Bash. Bash commands perform a wide range of tasks, such as:

  • Creating, moving, renaming, and deleting files and directories
  • Comparing and merging files
  • Editing and manipulating text within files
  • Compressing and decompressing files
  • Scheduling tasks to run automatically
  • Networking and remote access

The advantage of using Bash commands to navigate Linux lies in its flexibility and versatility. Linux is an open-source OS, and provides users with control over the underlying system. This allows the users to customize and configure it to their specific needs.

Читайте также:  Linux добавить группу файлу

By allowing users to tailor their environment to their specific needs, Bash and Linux enable a high level of personalization and efficiency. Some examples of possible customizations and configurations include aliases (custom shortcuts for frequently used commands or groups of commands), custom shell scripts to automate repetitive tasks, and options to fine-tune your environment variables to control the behavior of various applications running on your system.

This adaptability extends to Bash commands, which can be customized using bash scripts and command-line utilities. Linux offers a steady environment for executing Bash commands, ensuring that they can be ran consistently. This is especially vital in production environments where seamless automation and repeatability are a must.

In the following sections, we’ll introduce the most popular Bash commands, what they do, and how to extend them with options. Later on in this article, you’ll learn how to create your own custom commands (aliases), allowing you to create shortcuts for a single command or a group of commands.

When it comes down to it, if you don’t know the command line, you’re not using your computer to its full potential.

Check out the rest of this bash cheat sheet to get even more familiar with the command line concepts!

Источник

Bash Commands Cheat Sheet

Bash Commands

Bash is a command language interpreter: a version of the classic Unix shell with many enhancements. Bash is the default shell installed on GNU/Linux distributions and many other Unix-style systems, such as macOS.

Although most developers have a working knowledge of Bash for everyday interactive use, few know the rich features it offers for writing scripts. It supports most of the statements that other languages provide, such as indexed and associative arrays, foreground/background task execution, pipes, redirections, loops, functions, and Boolean expressions.

Читайте также:  Удалить docker linux mint

Download the Bash Commands Cheat Sheet to get started with Bash scripting. You’ll learn how to:

  • Create Bash scripts
  • Work with variables
  • Manipulate strings using parameter expansion
  • Create credentials
  • Control script flow with conditionals and loops
  • Work with collections, arrays, and maps

With Red Hat Developer cheat sheets, you get essential information right at your fingertips so you can work faster and smarter. Easily learn new technologies and coding concepts and quickly find the answers you need.

Excerpt

While loop

A while loop runs continuously until a certain condition is met. The following code uses the less then or equal to symbol -li to run a loop until the counter variable x reaches the number 5 .

x=1; while [ $x -le 5 ]; do echo "Hello World" ((x=x+1)) done

Copy, paste and run in your terminal:

Copy and paste the following code to create and run a Bash script that demonstrates a while loop.

cat while-loop-01.sh #!/usr/bin/env bash x=1; while [ $x -le 5 ]; do echo "Hello World" ((x=x+1)) done EOF bash while-loop-01.sh

Working with status codes

Reporting success and error in a Bash script is accomplished using status codes. By convention success is reported by exiting with the number 0 . Any number greater than 0 indicates an error. Also, there is a convention for error numbers that is explained in the article Bash command line exit codes demystified.

Using the exit keyword

The following demonstrates a Bash code that returns an error code 22 when the script is executed without a parameter.

if [ -z "$1" ]; then echo "No parameter"; exit 22; fi =

Copy, paste, and run in your terminal:

Copy and paste the following code to create and run a Bash script that returns an error code when the script is executed without a parameter.

cat status-code-01.sh #!/usr/bin/env bash if [ -z "$1" ]; then echo "No parameter"; exit 22; fi EOF bash status-code-01.sh echo $?

Источник

Оцените статью
Adblock
detector