Chapter 1: Hello World!
—
## Hello World: Our First Script
1 #!/usr/bin/env bash
2 echo "Hello World!"
Above script is stored in file welcome.sh
The shebang line /usr/bin/env bash tells which interpreter to use (in this case, bash) and also informs commands like file that welcome.sh is a Bash script.
Linux command file welcome.sh will show that it’s a bash script and an ASCII text executable. If we do not include the shebang line, the file command will not recognize that the file is a shell script.
It’s best practice to write the shebang line in a way that uses the shells environment to locate the bash executable. Not all systems have bash installed at the same place (/bin/bash for example). Using /usr/bin/env bash, tells whatever bash executable appears in current user’s $PATH variable.
Executing the script
—
Now that we have our first script, lets see how to execute it:
- Execute with
bash welcome.sh. No need to set the execute permission while executing this way. - To execute the script directly (i.e.
./welcome.sh), we need to set the execute permission,chmod a+x welcome.sh
umask
—
When a new file or directory is created, the permissions of that new object are determined by the default permissions for the file or directory. umask controls what permissions are not given to newly created file or directory. It does not affect the permissions of existing objects, only newly created objects.
- The default Unix permission set for newly created directories is
777 - The default permissions for newly created files is
666
Without the umask, all new directories would be created with full 777
permissions, and all new files would be created with full 666
permissions. The umask blocks certain permissions from being given to newly created file system objects.
Every bit set in the umask “masks”, or “takes away”, that permission from the default. “Mask” does not mean “subtract”, in the arithmetic sense.
Below is the octal representation of permissions:
-
4for Read -
2for Write -
1for Execute
Shell command umask 022 sets to —-w–w- permissions to be removed from the default permissions. Therefore, setting umask to this value will result in a file having 644 (rw-r—-r—) permission and directory having 755 (rwxr-xr-x) permission.
Using the chmod command without specifying whether you want to change User, Group, or Other permissions causes chmod to use your umask to decide what sets of permissions to change. If you want everyone (user, group, others) to have execute permissions, use chmod a+x file_name.sh.
Adding file to $PATH
—
In the $PATH environment variable, include the location where your script is and you can execute it by just specifying the name (welcome.sh). Else, use ./welcome.sh
Positional Parameters
—
-
$1, $2, $3....${10}: Upto 9, no need to use braces (we can if we want to). From 10 onwards, use braces to indicate it’s number ten and not 1 followed by 0. -
$0is NOT positional and represents the script name. -
$#represents the number of positional parameters that have been supplied. -
$*will list all parameters as a list, we can iterate through the same.