πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English | πŸ‡ͺπŸ‡Έ EspaΓ±ol | πŸ‡΅πŸ‡Ή PortuguΓͺs | πŸ‡ΉπŸ‡­ ΰΉ„ΰΈ—ΰΈ’ | πŸ‡¨πŸ‡³ δΈ­ζ–‡

What is Bash? A Beginner's Guide to the Shell and Its Power

When you're creating websites, you'll eventually encounter the "black screen"β€”the terminal (or CUI)β€”for things like server operations. For many designers and coders, this can feel intimidating... something to be avoided if possible. However, if you know just a little bit about Bash, the interactive program used on that black screen, your work efficiency can dramatically improve, and your world of web creation will expand.

In this article, we'll explain what Bash is and how to use it in simple, friendly terms so that even beginners to Linux and server operations can have that "Aha!" moment. We've prepared plenty of samples you can copy and paste to experience how it "just works," covering everything from essential file operations to basic techniques for automating tedious, repetitive tasks. Let's turn that fear of the black screen into the excitement of acquiring a new weapon!


First Off, What is a Shell? And What is Bash?

Before we talk about Bash, let's first understand its "boss," the shell.

The Shell: An "Interpreter" Connecting You to the OS

At the heart of a computer lies the kernel, a program that handles the most fundamental functions of the operating system, like managing hardware. Let's think of the kernel as a king. We can't give orders directly to the king (the kernel).

That's where the shell comes in. The shell is like an "interpreter" or a "secretary" who translates our words (commands) into a language the king can understand. When we type a command on the keyboard, the shell receives it, interprets it, and conveys it to the kernel to execute the order. That "black screen" is where this shell operates.

Bash: The Most Popular "Interpreter"

Just as there are many different kinds of interpreters, there are various types of shells (zsh, fish, csh, etc.). Among them, the one that is most widely and standardly used on Linux and macOS today is Bash (Bourne-Again SHell). Unless you have a specific reason otherwise, you can assume that the shell you'll be using on a server is almost certainly Bash. In other words, you can think of learning how to use Bash as the same thing as learning basic Linux command operations.


Let's Get Our Hands Dirty! Basic Bash Commands

Seeing is believing. Let's try out a few basic commands right away. Open the "Terminal.app" on macOS, or a terminal environment like WSL or Git Bash on Windows, then copy and paste the following commands and press Enter.

pwd - Check Your Current Location

pwd (Print Working Directory) is a command that tells you, "Which folder (directory) am I in right now?" If you ever feel lost, type this first.

$ pwd
/Users/yourname/Documents

ls - List Files and Folders Inside

ls (List) displays a list of files and folders in the current directory.

$ ls
ProjectA   ProjectB   memo.txt

Adding the -l option allows you to see more detailed information, such as file permissions, modification dates, and more.

$ ls -l
total 8
drwxr-xr-x  3 yourname  staff   96  Jul  5 10:00 ProjectA
drwxr-xr-x  5 yourname  staff  160  Jul  5 10:01 ProjectB
-rw-r--r--  1 yourname  staff  512  Jul  5 10:02 memo.txt

cd - Move to Another Folder

cd (Change Directory) is the command for moving to another directory. It feels like traveling from town to town in an RPG.

$ cd ProjectA

Specifying .. (two dots) allows you to go back up to the parent directory.

$ cd ..

Creating and Deleting Files and Folders

The basics of command-line operations are creating, moving, and deleting files and folders. With Bash, this is instantaneous.

mkdir - Create a New Folder

mkdir (Make Directory) creates a new, empty folder.

$ mkdir new-website

touch - Create a New Empty File

The touch command allows you to create an empty file. It's useful for creating template HTML or CSS files.

$ touch index.html

echo and > - Write Text to a File

echo is a command that outputs a specified string of text to the screen. When combined with the > (redirection) symbol, you can change the output destination from the screen to a file, thereby writing content to it.

$ echo "<h1>Hello, Bash!</h1>" > index.html

Note: > will overwrite the file. If you want to append to a file, use >> instead.


cat - Display the Contents of a File

You can use the cat command to display the contents of a file in the terminal. Let's check the content we just wrote.

$ cat index.html
<h1>Hello, Bash!</h1>

rm - Remove Files or Folders

rm (Remove) is a command to delete files and folders. This command is very powerful; it deletes files immediately and permanently without sending them to a trash bin, and it cannot be undone. Before using it, always double-check that you are deleting the right thing with pwd and ls.

$ rm index.html

To delete a folder and all its contents, you need the -r (Recursive) option.

$ rm -r old-website

The True Power of Bash! Chaining Commands with Pipes

One of Bash's most powerful features is the pipe |. This is a feature that "chains" the output of one command to become the input of the next command. Imagine a factory conveyor belt, passing items from one processing station to the next.

For example, if you want to "list all files in detail (ls -l), and then from that list, display only the lines containing the word 'index' (grep index)," you would chain them like this:

$ ls -l | grep "index"
-rw-r--r--  1 yourname  staff    23  Jul  5 10:30 index.html
-rw-r--r--  1 yourname  staff  1024  Jul  5 10:31 index.php

The beauty of Bash is that you can achieve complex operations in a single line by combining simple commands like this.


An Introduction to Bash Scripting for Automation

Typing the same combination of commands over and over is tedious, right? In such cases, you can save a series of commands to a file and execute them all at once. This is a Bash script.

For example, let's say every time you start a new website project, you always create the same folder structure (css, js, images) and an `index.html` file. Let's create a script named `create_project.sh` to automate this task.

#!/bin/bash
# Create a project folder with the name specified at runtime
PROJECT_NAME=$1
mkdir $PROJECT_NAME
cd $PROJECT_NAME

# Create various subfolders
mkdir css js images

# Create empty HTML and CSS files
touch index.html css/style.css

echo "Project '$PROJECT_NAME' created successfully!"

Create a file named `create_project.sh` with this content, then give it execute permissions with the `chmod +x` command.


$ chmod +x create_project.sh

After that, you just need to run it like `./create_project.sh project-name`, and the routine task is completed in an instant.

$ ./create_project.sh my-new-dream-site
Project 'my-new-dream-site' created successfully!

Conclusion

Great work! In this article, we learned about the basic concepts and usage of the shell, the program behind the black screen, and its most prominent representative, Bash. It might seem unapproachable at first, but by actually trying out a few of the commands introduced here, you've likely experienced a glimpse of its convenience and power.

For a web creator, Bash can be a weapon just as powerful as design tools or code editors. We encourage you to continue learning a few commands at a time and apply them to your daily work.

For those thinking, "But my Windows PC doesn't have Bash...", don't worry! In the next article, we'll explain how to install "WSL (Windows Subsystem for Linux)," which makes it easy to use Bash on Windows, with pictures to guide you!

[Use it on Windows!] How to Install Bash with WSL (with Pictures)