I have a bash script like:
#!/bin/bash
echo Hello world!
How do I execute this in Terminal?
$prompt: /path/to/script
and hit enter. Note you need to make sure the script has execute permissions.
This is an old thread, but I happened across it and I'm surprised nobody has put up a complete answer yet. So here goes...
Q: How do I execute this in Terminal?
Confusions and Conflicts:
sudo
to do any of this. This command is reserved for running commands as another user or a 'root' (administrator) user. Great post here.(A person who is just learning how to execute scripts should not be using this command unless there is a real need, like installing a new program. A good place to put your scripts is in your ~/bin folder. You can get there by typing cd ~/bin
or cd $HOME/bin
from the terminal prompt. You will have full permissions in that folder.)
To "execute this script" from the terminal on a Unix/Linux type system, you have to do three things:
Tell the system the location of the script. (pick one)
/path/to/script.sh
). You can verify the full path by typing pwd
or echo $PWD
in the terminal../
for the path (e.g. ./script.sh
). Easy.PATH
and just type the name (e.g. script.sh
). You can verify the system PATH
by typing echo $PATH
or echo -e ${PATH//:/\\n}
if you want a neater list.Tell the system that the script has permission to execute. (pick one)
chmod +x /path/to/script.sh
in the terminal.chmod 755 /path/to/script.sh
if you prefer numbers. There is a great discussion with a cool chart here.Tell the system the type of script. (pick one)
BASH /path/to/script.sh
or PHP /path/to/script.php
) If the script has an extension, such as .php or .py, it is part of the script name and you must include it.#!/bin/bash
) in your example. If you have that as the first line of your script, the system will use that program to execute the script. No need for typing programs or using extensions. PATH
by using #!/usr/bin/env
followed by the program name (e.g. #!/usr/bin/env bash
or #!/usr/bin/env python3
). There are pros and cons as thoroughly discussed here.Yet another way to execute it (this time without setting execute permissions):
bash /path/to/scriptname
If you are in a directory or folder where the script file is available then simply change the file permission in executable mode by doing
chmod +x your_filename.sh
After that you will run the script by using the following command.
$ sudo ./your_filename.sh
Above the "." represent the current directory. Note! If you are not in the directory where the bash script file is present then you change the directory where the file is located by using
cd Directory_name/write the complete path
command. Otherwise your script can not run.
Firstly you have to make it executable using: chmod +x name_of_your_file_script
.
After you made it executable, you can run it using ./same_name_of_your_file_script
cd to the directory that contains the script, or put it in a bin folder that is in your $PATH
then type
./scriptname.sh
if in the same directory or
scriptname.sh
if it's in the bin folder.