How to Write A Basic Bash Script?

7 minutes read

To write a basic Bash script, follow these steps:

  1. Open a text editor and create a new file with a .sh extension (e.g., script.sh).
  2. Start the script with a shebang, which tells the system to interpret the commands using Bash. Use "#!/bin/bash" at the beginning of your script.
  3. Define variables by assigning values to them. For example, "name='John'" or "age=25".
  4. Write commands to perform specific tasks. Bash scripts are a series of commands executed in order.
  5. Utilize control structures like loops and conditionals to perform actions based on certain conditions. For example, "if" statements or "for" loops.
  6. Save changes to the script file.
  7. Make the script executable by running the command "chmod +x script.sh" in the terminal.
  8. Execute the script by typing "./script.sh" in the terminal (assuming the script file is in the current directory).


Remember to add comments in your script to make it more understandable and maintainable. Comments start with "#" and are ignored by the interpreter.


This is just a brief overview of writing a basic Bash script. You can expand your script's functionality by exploring advanced concepts like functions, input/output handling, and error handling. Bash scripting allows you to automate tasks, execute commands, and build more complex scripts to suit your needs.

Best Linux Books to Read in 2024

1
Linux Bible

Rating is 5 out of 5

Linux Bible

2
Practical Linux Forensics: A Guide for Digital Investigators

Rating is 4.9 out of 5

Practical Linux Forensics: A Guide for Digital Investigators

3
The Linux Command Line, 2nd Edition: A Complete Introduction

Rating is 4.8 out of 5

The Linux Command Line, 2nd Edition: A Complete Introduction

4
How Linux Works, 3rd Edition: What Every Superuser Should Know

Rating is 4.7 out of 5

How Linux Works, 3rd Edition: What Every Superuser Should Know

5
CompTIA Linux+ Certification All-in-One Exam Guide, Second Edition (Exam XK0-005)

Rating is 4.6 out of 5

CompTIA Linux+ Certification All-in-One Exam Guide, Second Edition (Exam XK0-005)

6
CompTIA Linux+ Study Guide: Exam XK0-005 (Sybex Study Guide)

Rating is 4.5 out of 5

CompTIA Linux+ Study Guide: Exam XK0-005 (Sybex Study Guide)


How to check if a file or directory exists in Bash script?

In a Bash script, you can check if a file or directory exists using the -e option with the if statement.


To check if a file exists:

1
2
3
4
5
if [ -e /path/to/file.txt ]; then
    echo "File exists."
else
    echo "File does not exist."
fi


To check if a directory exists:

1
2
3
4
5
if [ -d /path/to/directory ]; then
    echo "Directory exists."
else
    echo "Directory does not exist."
fi


Note that the -e option can be used to check if a file or directory exists. If you specifically want to check for a file or a directory, you can use -f for a regular file and -d for a directory.


How to search for a string in a file using Bash script?

To search for a string in a file using a Bash script, you can make use of the grep command. Here's an example script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/bash

# Ask user for the string to search
echo "Enter the string to search:"
read searchString

# Ask user for the file path
echo "Enter the file path:"
read filePath

# Perform the search
grep "$searchString" "$filePath"


Save the script in a file, for example, search.sh, and make it executable by running chmod +x search.sh in the terminal. Then you can run the script by executing ./search.sh.


The script prompts the user to enter the string to search and the file path. It then performs the search using grep and prints out the lines containing the matching string from the file.


How to move files using Bash script?

To move files using a Bash script, you can use the mv command. The basic syntax for moving files is:

1
mv source_file destination


Here's an example of a Bash script that moves multiple files from one directory to another:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/bin/bash

# Source directory
source_dir="/path/to/source/directory"

# Destination directory
destination_dir="/path/to/destination/directory"

# Move files from source to destination directory
mv "$source_dir/"*.txt "$destination_dir/"

# Check if the move was successful
if [[ $? -eq 0 ]]; then
  echo "Files moved successfully."
else
  echo "Error in moving files."
fi


Replace /path/to/source/directory and /path/to/destination/directory with the actual paths to your source and destination directories, respectively.


In this script, *.txt is used to specify all files with the .txt extension in the source directory. You can modify this pattern to fit your specific file naming conventions.


Finally, the script checks the exit status of the mv command using $?. If the exit status is 0, it means the move was successful, and a success message is printed. Otherwise, an error message is printed.


Save the script with a .sh extension, make it executable with the chmod +x script.sh command, and then run it using ./script.sh in the terminal.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To redirect the output of a bash script to another file, you can use the ">" symbol followed by the filename. Here's how to do it:Open the terminal and navigate to the directory where your bash script is located. Use the following syntax to redi...
In Bash scripting, command-line arguments allow you to provide input and parameters to a script during execution. Here's an overview of how to handle command-line arguments in a Bash script:To access the command-line arguments, you can refer to them using ...
To run a bash script during a Docker run, you can follow these steps:Start by creating a Dockerfile. This file will define the build process for your Docker image. You can use a text editor to create this file. In the Dockerfile, specify the base image you wan...
To get a Bitbucket Auth token via a bash script, you can utilize the Bitbucket REST API for authentication. Here is a step-by-step guide on how to achieve this:Start by creating a personal access token on Bitbucket. Log in to your Bitbucket account, go to your...
To run a Selenium script from a bash file, you can follow the below steps:Install Selenium WebDriver: Begin by installing the Selenium WebDriver for your chosen programming language (Java, Python, etc.). You can use package managers like pip or maven, or downl...
To check if enable-bracketed-paste is enabled or disabled in Bash, you can use the following steps:Open the terminal or command prompt on your system.Type bash and hit Enter to launch the Bash shell.Enter bind -v | grep enable-bracketed-paste and press Enter.I...