How To Run Bash Script In Linux?
Bash is the heart of the Linux. Most of times 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 bash script executable and run in Linux
Script file holds required commands those will run in bash. Script file generally have .sh extension and file content starts with #!/bin/bash but keep in mind if the shell is not bash the interpreter will be change. Here is simple example shell file.
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 a permission to run files as command. Current user should have execution permission for the file.
$ chmod u+x myshell.sh
Actually the extension of the file is not important but using general conventions will makes administrators job easy. Following extensions can be used too;
Add Script To The PATH
We can call script file without giving full path and just call like a Linux command.Linux use $PATH variable to locate commands. We can add our script file path to the $PATH variable and call without full 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