How To Run Bash Script In Linux?


Bash is the heart of Linux. Most of the time bash commands are used to create a script and execute regularly. But the question is how can we run bash scripts in Linux in different ways? We will look different ways to make the bash script executable and run in Linux

Script File

Script file holds required commands those will run in bash. Script file generally has .sh extension and file content starts with #!/bin/bash but keeps in mind if the shell is not bashed the interpreter will change. Here is a simple example shell file.

#!/bin/bash 
 
echo "Hello Poftut"                                                                                                  
echo "Do some backup"

Make Sh File Executable

We can run shell script files directly like a command if we make them executable. Executable is permission to run files as a command. The current user should have execution permission for the file.

$ chmod u+x myshell.sh
Make Sh File Executable
Make Sh File Executable

Actually the extension of the file is not important but using general conventions will make administrators’ jobs easy. Following extensions can be used too;

  • SH
  • KSH

Add Script To The PATH

We can call a script file without giving the full path and just call like a Linux command. Linux bash has a $PATH variable to locate commands. We can add our script file path to the $PATH variable and call without full path.

$ PATH=$PATH:/home/ismail
Add Script To The PATH
Add Script To The PATH

Run Script With Bash Command

We can run script files without making them executable or change anything. bash command can be used to interpret the script file and run scripts inside it.

$ bash myshell.sh
Run Script With Bash Command
Run Script With Bash Command

1 thought on “How To Run Bash Script In Linux?”

Leave a Comment