Programming

Bash

What is Bash?

Bash is a shell: it reads a command, starts a program, and writes the result on the screen. The terminal is the window. Bash is the language inside it. On Ubuntu the default shell is usually Bash. On macOS it was Bash for a long time, and now it is often zsh. Most commands look alike in both.

What is Bash used for?

Bash is for finding a file, installing a program, and repeating the same command. It is also what you meet when you enter a server with SSH.

  • Seeing the files in a folder
  • Installing a package with apt
  • Keeping several commands in one script
  • Checking Nginx or Docker

What can you do with Bash?

With Bash you rename, copy a file, search text, and send one program's output into another. A script does the same job in the same order every time. Writing a windowed program is not its job.

Is Bash hard to learn?

Typing one command is easy. The difficulty is the space, the quote, and the permission. If a file name has a space, a command without quotes treats it as two files. sudo is not needed on every line. It is used on a command that changes the system.

Advantages and limits

Advantage: you do not repeat the same job by hand, and you work on a server without a window. Limit: a script may find a different command on another computer. A delete command is not undone. Check the name first, then delete.

Basic ideas

A command, an argument, and a flag are three parts. The | sign gives one command's result to the next.

  • pwd shows the current folder
  • ls lists the files
  • cd moves into a folder
  • echo writes text

A short example

These two lines store a name and write it on the screen. If the file will be a script, the first line is often #!/usr/bin/env bash.

name="Nibras"
echo "Hello, $name"

Common questions

  • Are the terminal and Bash the same? No. The terminal is the window. Bash is the command language inside it.
  • Is every Linux command Bash? No. ls and apt are separate programs. Bash starts them.
  • Does Bash write a site? No. It is for installing and checking, not for the page itself.