Linux: Bash

Bash

linux
bash
Bash Linux is a Unix shell and command language CLI (Command Language Interface)
Author

albertprofe

Published

Tuesday, June 1, 2021

Modified

Friday, November 1, 2024

1 Introduction

📘 Bash

Bash (short for “Bourne again shell”) is a Unix shell and command language which was the default shell on Unix systems at the time. Bash is a command-line interface (CLI) that allows users to enter commands and execute programs, as well as perform various other tasks such as managing files, manipulating text, and interacting with other programs.

Bash is the default shell on many Linux systems, as well as on macOS and other Unix-like operating systems.

2 Overview

Screenshot of Bourne Again SHell (BASH)

Screenshot of Bourne Again SHell (BASH)

Bash is a powerful and flexible tool that is widely used by developers, system administrators, and other users to automate tasks, write scripts, and perform other types of command-line operations.

It is particularly useful for managing and interacting with large numbers of files and directories, as well as for automating repetitive tasks.

Bash scripts can be used to automate a wide variety of tasks, such as setting up and configuring systems, performing backups, and managing network resources. Bash is also widely used as a programming language, with a syntax and structure that is similar to other programming languages.

In addition to its many features and capabilities, Bash is also highly customizable, with a variety of options and settings that allow users to customize its behavior and appearance. Users can customize their Bash environment by modifying the Bash configuration file, known as the bashrc file, which contains a set of commands and settings that are executed every time Bash is launched.

Screenshot of Bourne Again SHell (BASH) by Wikimedia Foundation

Screenshot of Bourne Again SHell (BASH) by Wikimedia Foundation

3 Basic command on Bash

Code Bash
introduction.sh
#!/usr/bin/env bash
# First line of the script is the shebang which tells the system how to execute
# the script: https://en.wikipedia.org/wiki/Shebang_(Unix)
# As you already figured, comments start with #. Shebang is also a comment.

# Simple hello world example:
echo "Hello world!" # => Hello world!

# Each command starts on a new line, or after a semicolon:
echo "This is the first command"; echo "This is the second command"
# => This is the first command
# => This is the second command

# Declaring a variable looks like this:
variable="Some string"

# But not like this:
variable = "Some string" # => returns error "variable: command not found"
# Bash will decide that `variable` is a command it must execute and give an error
# because it can't be found.

# Nor like this:
variable= "Some string" # => returns error: "Some string: command not found"
# Bash will decide that "Some string" is a command it must execute and give an
# error because it can't be found. In this case the "variable=" part is seen
# as a variable assignment valid only for the scope of the "Some string"
# command.

# Using the variable:
echo "$variable" # => Some string
echo '$variable' # => $variable
# When you use a variable itself — assign it, export it, or else — you write
# its name without $. If you want to use the variable's value, you should use $.
# Note that ' (single quote) won't expand the variables!
# You can write variable without surrounding quotes but it's not recommended.

# Parameter expansion ${...}:
echo "${variable}" # => Some string
# This is a simple usage of parameter expansion such as two examples above.
# Parameter expansion gets a value from a variable.
# It "expands" or prints the value.
# During the expansion time the value or parameter can be modified.
# Below are other modifications that add onto this expansion.

# String substitution in variables:
echo "${variable/Some/A}" # => A string
# This will substitute the first occurrence of "Some" with "A".

# Substring from a variable:
length=7
echo "${variable:0:length}" # => Some st
# This will return only the first 7 characters of the value
echo "${variable: -5}" # => tring
# This will return the last 5 characters (note the space before -5).
# The space before minus is mandatory here.

# String length:
echo "${#variable}" # => 11

# Indirect expansion:
other_variable="variable"
echo ${!other_variable} # => Some string
# This will expand the value of `other_variable`.

# The default value for variable:
echo "${foo:-"DefaultValueIfFooIsMissingOrEmpty"}"
# => DefaultValueIfFooIsMissingOrEmpty
# This works for null (foo=) and empty string (foo=""); zero (foo=0) returns 0.
# Note that it only returns default value and doesn't change variable value.

# Declare an array with 6 elements:
array=(one two three four five six)
# Print the first element:
echo "${array[0]}" # => "one"
# Print all elements:
echo "${array[@]}" # => "one two three four five six"
# Print the number of elements:
echo "${#array[@]}" # => "6"
# Print the number of characters in third element
echo "${#array[2]}" # => "5"
# Print 2 elements starting from fourth:
echo "${array[@]:3:2}" # => "four five"
# Print all elements each of them on new line.
for item in "${array[@]}"; do
    echo "$item"
done

4 Cheat-sheet

Back to top