How to create and run a script in Ubuntu |Video upload date:  · Duration: PT2M58S  · Language: EN

Learn how to create make executable and run a script in Ubuntu using the terminal with simple commands and examples

If you can type and you have mild control issues with your computer then shell scripting is the tiny addiction that becomes very useful. This guide walks through creating a runnable script in Ubuntu using the terminal and a minimum of ceremony, with honest explanations and a few jokes at your future self.

Start by making a file and adding a shebang

Create a new text file with your editor of choice. Yes nano is fine, yes vim is a flex, and yes you can use touch if you prefer pretending the file made itself.

nano hello.sh
# or
touch hello.sh && nano hello.sh

Put a shebang at the top so the kernel knows which interpreter to call. A portable and common choice is this line.

#!/usr/bin/env bash

echo "Hello world"

The shebang tells Linux to run the rest of the file with bash. If you skip it the file is still text and you can run it by invoking an interpreter explicitly, but including the shebang makes your script behave like a tiny program.

Make the script executable with chmod

Files need the execute bit set before the kernel will let you run them directly. This is not witchcraft, it is file permissions.

chmod +x hello.sh

That grants execute permission for the file owner. You can confirm with ls -l to see the x flag in the mode string. If you need other users to run it adjust permissions or install it into a shared directory.

Run the script from the terminal

Run it from the current directory like this.

./hello.sh

Or call an interpreter explicitly which ignores the file executable bit.

bash hello.sh

If you put the script in a directory that is in your PATH then you can run it by name from anywhere. Common choices are ~/bin or /usr/local/bin depending on whether you want a single user or system wide tool.

What to do when things fail

  • Check the shebang for typos
  • Verify the execute bit with ls -l
  • Run with bash explicitly to get interpreter errors
  • Add set -e near the top to stop on the first error and make failures less mysterious
  • Add set -x temporarily to trace what the script is running

Small best practices that save headaches

  • Use #!/usr/bin/env bash for portability across Linux systems
  • Keep scripts small and focused so debugging is not a scavenger hunt
  • Use meaningful names and put scripts for frequent use in your PATH
  • Comment intent not every line, just the weird bits that future you will curse about

Quick recap checklist

  • Create a file in the terminal or editor
  • Add a shebang such as #!/usr/bin/env bash
  • Add commands like echo and whatever automation you need
  • Run chmod +x to make it executable
  • Execute with ./scriptname or bash scriptname or from PATH

There you go. You turned a plain text file into a small automation that saves keystrokes and gives you a tiny sense of mastery over Ubuntu, bash, and the terminal. Be kind to your scripts and they will be kinder to your future self.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.