I have a bash script like:
#!/bin/bash
echo Hello world!
How do I execute this in Terminal?
Yet another way to execute it (this time without setting execute permissions):
bash /path/to/scriptname
/(your conda installation path)/bin/conda init zsh) and then close and reopen the shell one more timebash before the path?chmod +x) and has a shebang (e.g. the first line is similar to #!/bin/bash) then the interpreter needs to be specified. Thus bash (or another interpreter) is used.$prompt: /path/to/script and hit enter. Note you need to make sure the script has execute permissions.
/path/to directory, e.g. with the cd /path/to command, you can enter ./script to run your script.Don't forget, in this case the './' before '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.
./scriptname.sh works for me but scriptname.sh gives scriptname.sh: command not found. -rwxr-xr-x are its permissions.cd anywhere at all perpetrates another common beginner misunderstanding. Unless the script internally has dependencies which require it to run in a particular directory (like, needing to read a data file which the script inexplicably doesn't provide an option to point to) you should never need to cd anywhere to run it, and very often will not want to.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...
The answer is below, but first ... if you are asking this question, here are a few other tidbits to help you on your way:
ls may be built-in's, but most commands are actually separate small programs. (This is where the "Zen of Unix" comes in ... "(i) Make each program do one thing well.")echo $PATH to see), then you must include it. If you want to run a script from the current directory, use ./ before it. This ./ thing means 'here in the current directory.'sudo to do any of this. This command is reserved for running commands as another user or a 'root' (administrator) user. Running scripts with sudo allows much greater danger of screwing things up. So if you don't know the exact reason for using sudo, don't use it. Great post here.~/bin folder.# A good place to put your scripts is in your ~/bin folder.
> cd ~/bin # or cd $HOME/bin
> ls -l
You will see a listing with owners and permissions. You will notice that you 'own' all of the files in this directory. You have full control over this directory and nobody else can easily modify it.
If it does not exist, you can create one:
> mkdir -p ~/bin && cd ~/bin
> pwd
/Users/Userxxxx/bin
# type the name of the script with the full path
> /path/to/script.sh
# execute the script from the directory it is in
> ./script.sh
# place the script in a directory that is on the PATH
> script.sh
# ... to see the list of directories in the path, use:
> echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# ... or for a list that is easier to read:
> echo -e ${PATH//:/\\n}
# or
> printf "%b" "${PATH//:/\\n}"
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
# set the 'execute' permissions on the script
> chmod +x /path/to/script.sh
# using specific permissions instead
# FYI, this makes these scripts inaccessible by ANYONE but an administrator
> chmod 700 /path/to/script.sh
# set all files in your script directory to execute permissions
> chmod +x ~/bin/*
There is a great discussion of permissions with a cool chart here.
> bash /path/to/script.sh
...
> php /path/to/script.php
...
> python3 /path/to/script.py
...
#!/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.Note: This "portable" shebang may not be as portable as it seems. As with anything over 50 years old and steeped in numerous options that never work out quite the way you expect them ... there is a heated debate. The most recent one I saw that is actually quite different from most ideas is the "portable" perl-bang:
#!/bin/sh
exec perl -x "$0" "$@"
#!perl
script.sh and script.php are the literal file names of these scripts, and that if you put an extension in the file name, you need to include it when you run the script (and vice versa; if the script doesn't have an extension, don't put one). This is unlike e.g. DOS where you could omit the .bat or .exe from the file name, and files needed to have an extension from a small set in order to be considered executable.You could do:
sh scriptname.sh
sh is a symlink to bash, or if the script does not use any Bash-specific construct. In the former case, using bash instead of sh is the only correct, portable solution; in the latter case, it's not the correct answer to this particular question, because the OP asked about advice for a Bash script specifically. Perpetrating the wrong answer is irresponsible; users who fail to grasp the difference frequently post here, and need to be shown why this "worked for me" answer did not work for them.Change your directory to where script is located by using cd command
Then type
bash program-name.sh
cd anywhere; you can speciy an arbitrarily complex path name as the argument to bash; indeed, any program which takes a file name argument works this way (or is terrifyingly, mind-numbingly broken).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.
$ bash ./your_filename.sh
Above the "." represent the current directory.
sudo is wrong and potentially horribly wrong if you use it on an untrusted script which abuses the powers you needlessly gave it.cd somewhere is hard to understand, but probably also wrong.bash in the second line after specifiying the permissions in the first line with chmod.
chmod +x scriptnameand then./scriptname.