--- title: Scripting in Bash and other sh-es 01 categories: [linux, tech] tags: [scripting, bash] --- # Say Hello Universe in Bash For sure, I do not want to go against the culture and start the series without a 'Hello world' kind of introduction. In this part of the series, I purpose to understand: - [ ] Wtf is a shell -a computer shell for that matter? - [ ] Who's Bash? - [ ] How to write a bash script and make it executable - [ ] Say Hello Universe in Bash. ### Wtf is a shell A shell is a computer program that exposes an operating system's services to a human user or other programs. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer's role and particular operation. It is named a shell because it is the outermost layer around the operating system ### Who's Bash Command-line shells require the user to be familiar with commands and their calling syntax, and to understand concepts about the shell-specific language. Bash and other sh-es are examples of such calling syntax making them shell languages. ### How to write a bash script and make it executable. Having understood a shell and a shell-language, it follows that a script is a series of valid syntaxes that are excuted whilst in a file. Such a file, is what is reffered to as a script. If the shell language used is Bash...it becomes a Bash Script. There are two typical ways to write a shell script. 1. The not so proper way 2. The proper way #### The not so proper way As earlier mentioned, a script is a file. The not so proper way, is creating a file with the `.sh` extension. :::success Example: Use your fav editor. I'll be using vim. ```shell vim script.sh ``` Now, we put some shell commands to the file. - if new to linux, you'll have to know some [basic commands](https://swcarpentry.github.io/shell-novice/reference.html) Make it executable by adding the `x` permission to the owner/user. ```shell! sudo chmod u+x script.sh ``` Run the script by: ```shell! ./script.sh ``` ::: #### The proper way - The shebang way. Similary to the not so proper way, we start by creating a file.However, we need not include the extension `.sh` :::success ```shell! vim script ``` ::: Uniquely, the first line of the file is the shebang, followed by the binary to execute the script. :::info ! A shebang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang,pound-bang, or hash-pling. ::: In our case, the following will be our first line. Considering the shebang and the bash binary. ```shell! #!/bin/bash ``` :::success Example: A proper script to list storage and check the working dir would look like: ```shell! #!/bin/bash ls pwd ``` Make it executable by adding the `x` permission to the owner/user. ```shell! sudo chmod u+x script ``` Run the script by: ```shell! ./script.sh ``` ::: ### Say Hello Universe in Bash After all is read and understood, it's now time to say Hello Universe in Bash. The following proper script if ran would print 'Hello Universe' ```shell! #!/bin/bash echo 'Hello Universe' ``` Bye. To the next ...