Our first bash script will be a simple script that will output the line "Hello Friend!" to the screen. In Elliot's home directory, create a file named hello.sh and insert the following two lines:
elliot@ubuntu-linux:~$ cat hello.sh
#!/bin/bash
echo "Hello Friend!"
Now we need to make the script executable:
elliot@ubuntu-linux:~$ chmod a+x hello.sh
And finally, run the script:
elliot@ubuntu-linux:~$ ./hello.sh
Hello Friend!
Congratulations! You have now created your first bash script! Let's take a minute here and discuss a few things; every bash script must do the following:
- #!/bin/bash
- Be executable
You have to insert #!/bin/bash at the first line of any bash script; the character sequence #! is referred to as a shebang or hashbang and is followed by the path of the bash shell.