In your favorite text editor or IDE, create an empty file named hello_world in a new project directory. This file has no extension. Because of that your editor might not recognize this is a Bash script and won't do proper syntax highlighting.
To change that, you need to change the syntax highlighting setting for the file.
Atom
In Atom click Plain Text at the bottom right of the window.
You'll then get a menu. Type sh and you'll get the menu item, Shell Script.
Once you select Shell Script, your status bar will change and syntax highlighting will work correctly.
Visual Studio Code
In VSCode click Plain Text at the bottom right of the window.
You'll then get a menu. Type she and you'll get the menu item, Shell Script.
Once you select Shell Script, your status bar will change and syntax highlighting will work correctly.
Now that you've created your empty file, lets fill it with the following:
#!/bin/bash
echo "Hello World"
The first line is called a Unix Shebang. It starts with a crunch (#) and a bang (!) character and then tells Unix to run this script using /bin/bash. Basically, this means it will run the code in the script using your shell (/bin/bash).
The second line of the script actually outputs the text inbetween the quotes "Hello World" to Standard Output.
Before we can run this script, we have to tell Unix that it's an executable script. In your terminal, switch to the directory that contains your script:
cd /path/to/script
Then run:
chmod +x hello_world
What does this last command do? This changes the Unix permissions from a standard file to an executable file. Observe the following:
Before chmod +x hello_world
ls -l
total 4
-rw-rw-r-- 1 dev dev 38 Jan 26 13:20 hello_world
After chmod +x hello_world
ls -l
total 4
-rwxrwxr-x 1 dev dev 38 Jan 26 13:20 hello_world
You'll notice that the file permission line has x in it (-rwxrwxr-x), where before it had - (-rw-rw-r--). This means that the file has been changed to executable.
Your output might look slightly different, but you should still see the x in the output.
How do we run our new script? We can't just type hello_world. The reason we can't is because Unix will try executing a command outside of our directory. We need to be specific and execute our command with it's location.
./hello_world
Hello World
You'll notice that we prepended a ./ (dot slash) before the hello_world.
If we tried executing our new script without the dot slash, we might get something like:
hello_world
Command 'hello_world' not found, did you mean:
command 'hello-world' from snap hello-world (6.4)
In this case, Unix wants us to be specific.
What does the dot slash mean? It means the current directory. You can also execute commands with it's absolute path, such as:
/path/to/hello_world
Hello World
On my system, I have my hello_world script located in /home/dev/lessons/shell.
/home/dev/lessons/shell/hello_world
Hello World
You can also execute scripts from relative directories. For example, on my system I can change my directory up one level:
cd ..
Then I can execute:
shell/hello_world
Hello World
We can add your command to the global system, but we'll do that at another time.