Create a file first_script.sh
#!/bin/bash
# This is our first script.
echo 'Hello World!'
and run it as
$ bash first_script.sh
Hello World!
But to execute the file without bash command we need to set the right permission on file (permission to make file executable).
$ ls -l first_script.sh
-rw-rw-r-- 1 himanshu himanshu 59 Oct 19 22:16 first_script.sh
$ chmod 755 first_script.sh
$ ls -l first_script.sh
-rwxr-xr-x 1 himanshu himanshu 59 Oct 19 22:16 first_script.sh
$ ./first_script.sh
Hello World!
There are two common permission settings for scripts; 755 for scripts that everyone can execute, and 700 for scripts that only the owner can execute. Note that scripts must be readable in order to be executed.
If we like to run it just by putting the name of file and not the prefix ./
then we need to either place the file in any of the locations PATH have or update the PATH to have current file location included in PATH.
$ echo $PATH
/home/himanshu/.local/bin:/home/himanshu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/usr/local/go/bin:/snap/bin
$ export PATH=$PWD:"$PATH"
$ first_script.sh
Hello World!