Skip to main content
ubuntuask.com

Back to all posts

How to Write A Basic Bash Script?

Published on
4 min read
How to Write A Basic Bash Script? image

Best Bash Scripting Guides to Buy in October 2025

1 The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting

The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting

BUY & SAVE
$27.89 $49.99
Save 44%
The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting
2 Bash Pocket Reference: Help for Power Users and Sys Admins

Bash Pocket Reference: Help for Power Users and Sys Admins

BUY & SAVE
$17.60 $24.99
Save 30%
Bash Pocket Reference: Help for Power Users and Sys Admins
3 Advanced Bash Scripting Guide - Volume 2: An in-depth exploration of the art of shell scripting ( Revision 10 )

Advanced Bash Scripting Guide - Volume 2: An in-depth exploration of the art of shell scripting ( Revision 10 )

BUY & SAVE
$29.99
Advanced Bash Scripting Guide - Volume 2: An in-depth exploration of the art of shell scripting ( Revision 10 )
4 BASH Guide

BASH Guide

BUY & SAVE
$0.99
BASH Guide
5 Bash Quick Start Guide: Get up and running with shell scripting with Bash

Bash Quick Start Guide: Get up and running with shell scripting with Bash

BUY & SAVE
$32.99
Bash Quick Start Guide: Get up and running with shell scripting with Bash
6 Classic Shell Scripting

Classic Shell Scripting

BUY & SAVE
$24.73 $49.99
Save 51%
Classic Shell Scripting
7 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$36.76 $49.95
Save 26%
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
8 Super Easy Linux / Bash: The Ultimate Guide to Linux Commands and Bash Scripting: From Beggining to Advanced (Super Easy General Programming)

Super Easy Linux / Bash: The Ultimate Guide to Linux Commands and Bash Scripting: From Beggining to Advanced (Super Easy General Programming)

BUY & SAVE
$22.00
Super Easy Linux / Bash: The Ultimate Guide to Linux Commands and Bash Scripting: From Beggining to Advanced (Super Easy General Programming)
+
ONE MORE?

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.

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:

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

To check if a directory exists:

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:

#!/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:

mv source_file destination

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

#!/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.