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.

  1. Open a text editor, such as Gedit or Nano.
  2. Write script code, for example:
#!/bin/bash

echo "Hello, Ubuntu!"
  1. .sh file extension
  2. File named script.sh
  3. Open the terminal and navigate to the directory where the script is located.
  4. Run the following command to add executable permission:
chmod +x script.sh
  1. Execute the script using the following command:
./script.sh

Option 2: Write and execute scripts directly in the terminal

  1. Open the terminal.
  2. Use the following command to create a new script file and open a text editor:
nano script.sh
  1. Write script code, for example:
#!/bin/bash

echo "Hello, Ubuntu!"
  1. Cut
  2. Use the following command to add execute permission:
chmod +x script.sh
  1. 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.

bannerAds