How to create a bash file in Linux?

To create a bash file, the first step is to open the terminal. Then follow the steps below:

  1. The script named my_script.sh
vi my_script.sh
  1. Enter the content of the bash script in the open text editor. For example, you can input the following:
#!/bin/bash

echo "Hello, World!"

This is a simple example script that will output “Hello, World!”.

  1. Save the file and exit the text editor. In vi, you can press the Esc key, then type :wq and press Enter to save and exit.
  2. Grant the script executable permission. Use the chmod command in the terminal to give the script execute permission. The command format is:
chmod +x my_script.sh

This will allow you to run the script.

You can now run the script. In the terminal, execute the following command:

./my_script.sh

You should be able to see “Hello, World!” printed on the screen.

bannerAds