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

Introduction to Bash Scripting: Simple Automation Script Examples to Write First

In the last article, we learned the basic "words" (commands) of Bash, like cd and ls. While typing individual commands is useful, the true power of Bash lies in combining these words to create "sentences" or "stories," and having the computer do the work for you automatically. This is what a Bash script is.

Do you have tedious, repetitive tasks you do all the time? "Setting up the same folder structure for every project," "resizing multiple images at once," "periodically backing up files"... By learning Bash scripting, you can make tasks like these finish in an instant, as if by magic.

This article is for beginners who have gotten a little used to command-line operations. We'll explain everything from the basic way to write a Bash script to programming fundamentals like variables, conditional branching (if statements), and loops (for loops), all with simple automation script examples that you can copy and paste to get them working. Let's create your own handy automation tools, just like writing out a recipe!


1. Your First Bash Script - The First Step in Automation

A Bash script is just a simple text file with a sequence of commands you want to execute in order. You don't need any special tools to create one. Let's make a simple script and run it right away.

Step 1: Create the Script File

First, create a text file named hello_script.sh and write the following content in it. It's common practice to use the .sh extension for script files.

#!/bin/bash

# Display a message on the screen
echo "Hello, welcome to the world of Bash scripting!"
echo "The current date and time is:"
date

Think of the first line, #!/bin/bash, as a "magic spell." It's a special line (called a shebang) that tells the system, "Hey, please execute this file with Bash."


Step 2: Grant Execute Permission

A newly created script file is not yet recognized as an "executable program." Let's use the chmod command to give the file execute permission.

$ chmod +x hello_script.sh

Step 3: Run the Script

You're all set! Let's run the script with the following command. The ./ means "in the current directory."

$ ./hello_script.sh
Hello, welcome to the world of Bash scripting!
The current date and time is:
Sun Jul  6 09:47:45 JST 2025

The commands you wrote were executed in order. This is the basic principle of a Bash script!


2. Variables and Arguments - Making Scripts Flexible

A script that can only do one fixed thing isn't very interesting. So, let's use "variables" and "arguments" to make our scripts more flexible.

Variables: "Boxes" for Storing Data

A variable is a "named box" where you can store any data you like (text strings or numbers). You define one with the format VARIABLE_NAME=value and use it by writing $VARIABLE_NAME.

#!/bin/bash

# Store a string in a variable named GREETING
GREETING="Welcome,"
USER_NAME="Alice"

# Display a message using the variables
echo "$GREETING $USER_NAME!"

Arguments: Giving Information to a Script from the Outside

It's a pain to rewrite the inside of a script every time you want to run it. That's where "arguments" come in. This is a mechanism for passing information to the script from the outside when you run it. You can receive this information with $1, $2, and so on, by number.

Let's create the following script named greet.sh.

#!/bin/bash

# Store the first argument from runtime in the USER_NAME variable
USER_NAME=$1

echo "Hello, $USER_NAME!"

When you run this script, you run it with a name after it.


$ ./greet.sh Bob
Hello, Bob!

As you can see, using arguments makes scripts much easier to reuse.


3. Conditional Branching with 'if' Statements - Changing Actions Based on Conditions

A fundamental concept in programming, "if this, then that," can also be easily written in a Bash script. For this, we use an if statement.

For example, let's create a script that checks whether a specified file exists.

#!/bin/bash

FILE_NAME=$1

# if a "file" with the name $FILE_NAME exists, then
if [ -f "$FILE_NAME" ]; then
  echo "File '$FILE_NAME' was found."
else
  echo "File '$FILE_NAME' does not exist."
fi

The [ -f "filename" ] is the conditional expression that checks, "Does that file exist?" Let's run this script.


If the file exists:

$ touch sample.txt
$ ./check_file.sh sample.txt
File 'sample.txt' was found.

If the file does not exist:

$ ./check_file.sh another.txt
File 'another.txt' does not exist.

4. Repetitive Tasks with 'for' Loops - Knocking Out Tedious Work

The essence of automation lies in "repetition." If you have to repeat the same kind of task many times, let the computer do it for you. For that, the for loop is convenient.

Let's look at an example script that creates multiple HTML files at once as a template for a website.

#!/bin/bash

# Repeat a process for the names about, works, and contact
for page in about works contact
do
  # Dynamically generate the filename and H1 tag using the page variable
  FILE_NAME="${page}.html"
  TITLE_TEXT=$(echo "$page" | sed 's/./\u&/') # Capitalize the first letter

  # Use a heredoc to write the entire HTML at once
  cat << EOF > $FILE_NAME
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>$TITLE_TEXT Page</title>
  <style>
    body { font-family: sans-serif; line-height: 1.6; color: #333; }
    h1 { color: #0056b3; }
  </style>
</head>
<body>
  <h1>This is the $TITLE_TEXT page.</h1>
  <p>Content for the $page page goes here.</p>
</body>
</html>
EOF

  echo "Created ${FILE_NAME}."
done

echo "All pages created!"

This script performs a loop for the three words `about`, `works`, and `contact`, and generates a complete HTML file for each name. The part from << EOF to EOF is called a heredoc, and it's useful for outputting multi-line strings as-is.


Let's run it.

$ ./create_pages.sh
Created about.html.
Created works.html.
Created contact.html.
All pages created!

Let's check the contents of a generated file.

$ cat about.html
<!DOCTYPE html>
<html lang="en">
...
  <h1>This is the About page.</h1>
...
</html>

Brilliantly, three HTML files, each with different content, were created in an instant!


Conclusion

This time, as a first step into the world of Bash scripting, we looked at basic syntax and simple automation examples.

Scripting might sound difficult, but in the end, it's nothing more than "writing down the commands you always type by hand, in order." First, try thinking about whether you can turn the tasks you often do into a script. That small step should free you from tedious work and let you focus on more creative tasks.

Now that you've learned the basic way to write scripts, you'll probably want to learn more practical and "good" ways of writing them, and tips for avoiding common beginner mistakes. In the next article, we'll explain these best practices.

[The Definitive Guide] 5 Bash Patterns to Learn and How to Avoid Mistakes