How to write and run Ubuntu scripts?
There are multiple ways to write and execute scripts on Ubuntu, below we will introduce two commonly used methods.
Option 1: Write a script file using a text editor.
- Open a text editor, such as Gedit or Nano.
- Write script code, for example:
#!/bin/bash
echo "Hello, Ubuntu!"
- .sh file extension
- File named script.sh
- Open the terminal and navigate to the directory where the script is located.
- Run the following command to add executable permission:
chmod +x script.sh
- Execute the script using the following command:
./script.sh
Option 2: Write and execute scripts directly in the terminal
- Open the terminal.
- Use the following command to create a new script file and open a text editor:
nano script.sh
- Write script code, for example:
#!/bin/bash
echo "Hello, Ubuntu!"
- Cut
- Use the following command to add execute permission:
chmod +x script.sh
- Run the script by executing the following command:
./script.sh
The above method can help you write and execute scripts. Depending on your needs, you can use a different text editor or save the script files in a different directory.