How to Run a Shell Script With Just Using the Script Name in Linux
This article will explain how to run a shell script by using the script name in Linux.
First, we will set the file as executable. Next, we will examine ways to run the script with the name only.
We have example.sh
in $HOME/folder/example.sh
. Here is the content of the shell script file.
echo "Hello, World!"
Make File Executable in Bash
- Add
shebang
at the beginning of the script.
#!/bin/bash
- Add
execute
permission to the file.
sudo chmod +x example.sh
Run the Script Without Typing bash
or sh
We can use different methods to run a shell script from anywhere in the Linux terminal without adding bash
or sh
commands.
Move the Script to Under the $PATH
Move the script to a directory under the $PATH
like /usr/local/bin
.
sudo mv $HOME/folder/example.sh /usr/local/bin
Use the install
Command in Linux
The install
command can also move the script to a directory under the $PATH
.
sudo install example.sh /usr/local/bin/example
Add the Script Directory to the $PATH
Add the script directory ($HOME/folder/
) to the $PATH
in .bashrc
PATH="$PATH:$HOME/folder/"
Use source
to update Bash.
source ~/.bashrc
Create alias
for the Script Execute Command
Create an alias
that points to the full command to run the script.
alias example.sh="bash $HOME/folder/example.sh"
Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.
LinkedIn