This post introduces how to writ and run a bash file from terminal on Ubuntu. (If you prefer video style tutorials, check here for a post that is video based.)
The bash ( Bourne Again Shell) is the most common shell installed with Linux distributions and Mac OS.
- Write a bash file
the most common is to write a file, make sure the first line is
#!/bin/bash
Then save the file. Next mark it executable using chmod +x file
Then when you click (or run the file from the terminal) the commands will be executed. By convention these files usually have no extension, however you can make then end in .sh
or any other way.
For example,
#!/bin/bash echo Hello World
A Simple Bash Example
#!/bin/bash
echo "This is a shell script"
ls -lah
echo "I am done running ls"
SOMEVAR='text stuff'
echo "$SOMEVAR"
- Run a bash file
go to the folder where you bash file is located and type:
./yourbashfile.sh
./
just means that you should call the script located in the current directory. (Alternatively, just type the full path of the yourbashfile.sh). If it doesn’t work then, check if yourbashfile.sh has execute permissions.
You can add execute permission by the following command:
$ chmod +x yourbashfile.sh
References:
- Bash (Unix shell)
- Shell script
- Bash scripting Tutorial (pdf) – very concise and comprehensive tutorials
- Bash Guide for Beginners (ch2 Creating and running a script)
- A quick guide to writing scripts using the bash shell (pdf)
- Writing Your First Script And Getting It To Work
- The 5-Minute Essential Shell Tutorial (PDF)
- Learning the shell
Why Bother?
Why do you need to learn the command line anyway? Well, let me tell you a story. A few years ago we had a problem where I used to work. There was a shared drive on one of our file servers that kept getting full. I won’t mention that this legacy operating system did not support user quotas; that’s another story. But the server kept getting full and it stopped people from working. One of our software engineers spent the better part of a day writing a C++ program that would look through all the user’s directories and add up the space they were using and make a listing of the results. Since I was forced to use the legacy OS while I was on the job, I installed a Linux-like command line environment for it. When I heard about the problem, I realized I could do all the work this engineer had done with this single line:
du -s * | sort -nr > $HOME/user_space_report.txt
Graphical user interfaces (GUIs) are helpful for many tasks, but they are not good for all tasks. I have long felt that most computers today are not powered by electricity. They instead seem to be powered by the “pumping” motion of the mouse! Computers were supposed to free us from manual labor, but how many times have you performed some task you felt sure the computer should be able to do but you ended up doing the work yourself by tediously working the mouse? Pointing and clicking, pointing and clicking.
I once heard an author say that when you are a child you use a computer by looking at the pictures. When you grow up, you learn to read and write. Welcome to Computer Literacy 101. Now let’s get to work.
Contents
- What Is “The Shell”?
- Navigation
- Looking Around
- A Guided Tour
- Manipulating Files
- Working With Commands
- I/O Redirection
- Expansion
- Permissions
- Job Control
Here Is Where The Fun Begins
With the thousands of commands available for the command line user, how can you remember them all? The answer is, you don’t. The real power of the computer is its ability to do the work for you. To get it to do that, we use the power of the shell to automate things. We write shell scripts.
What Are Shell Scripts?
In the simplest terms, a shell script is a file containing a series of commands. The shell reads this file and carries out the commands as though they have been entered directly on the command line.
The shell is somewhat unique, in that it is both a powerful command line interface to the system and a scripting language interpreter. As we will see, most of the things that can be done on the command line can be done in scripts, and most of the things that can be done in scripts can be done on the command line.
We have covered many shell features, but we have focused on those features most often used directly on the command line. The shell also provides a set of features usually (but not always) used when writing programs.
Scripts unlock the power of your Linux machine. So let’s have some fun!
Contents
- Writing Your First Script And Getting It To Work
- Editing The Scripts You Already Have
- Here Scripts
- Variables
- Command Substitution And Constants
- Shell Functions
- Some Real Work
- Flow Control – Part 1
- Stay Out Of Trouble
- Keyboard Input And Arithmetic
- Flow Control – Part 2
- Positional Parameters
- Flow Control – Part3
- Errors And Signals And Traps (Oh My!) – Part 1
- Errors And Signals And Traps (Oh My!) – Part 2
- Beginners/BashScripting
- BASH Programming – Introduction HOW-TO
- How do I run .sh file in terminal?
- how to execute a python program in a shell script
https://askubuntu.com/questions/223691/how-do-i-create-a-script-file-for-terminal-commands/223698
http://stackoverflow.com/questions/17015449/how-do-i-run-sh-or-bat-files-from-terminal
Ubuntu – Shell script to execute/run (pdf)
wikiHow to Write a Shell Script Using Bash Shell in Ubuntu
How to create & execute a script file [closed]
Advanced Bash-Scripting Guide (An in-depth exploration of the art of shell scripting) by Mendel Cooper