To run a Bash script, you can follow these steps:
- Create a new file and save it with a
.sh
extension. For example,myscript.sh
. - Open the file in a text editor and write your Bash script within it. Here’s an example:
#!/bin/bash
echo "This is my Bash script."
echo "Today's date is $(date)."
In this example, the script prints a message and displays the current date using the date
command.
- Save the file.
- Open a terminal or command prompt.
- Navigate to the directory where your script is located. For example, if your script is in the
/home/user/scripts
directory, you can navigate there using the command:
cd /home/user/scripts
6. Make the script executable by running the command:
chmod +x myscript.sh
This command grants the script execution permission.
7. Finally, run the script by typing its name preceded by ./
(dot slash), followed by Enter:
./myscript.sh
The script will execute, and you should see the output in the terminal.
Note: Ensure that you have the Bash interpreter installed on your system. Bash is typically available by default on Unix-like operating systems, including Linux. If you’re using a different operating system, you may need to install Bash or use an alternative shell that supports Bash scripting.