Running bash scripts linux

How do I execute a bash script in Terminal?

Yet another way to execute it (this time without setting execute permissions):

had a seperate issue (trying to install anaconda via terminal on mac) and this was the solution. only thing I had to do was close and restart the shell, follow the one step here ( /(your conda installation path)/bin/conda init zsh ) and then close and reopen the shell one more time

$prompt: /path/to/script and hit enter. Note you need to make sure the script has execute permissions.

If you already are in the /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

if in the same directory or

./scriptname.sh works for me but scriptname.sh gives scriptname.sh: command not found . -rwxr-xr-x are its permissions.

The advice to 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 Executing a Command Line Script Tutorial!

Q: How do I execute this in Terminal?

The answer is below, but first . if you are asking this question, here are a few other tidbits to help you on your way:

Confusions and Conflicts:

The Path

  • Understanding The Path (added by tripleee for completeness) is important. The «path» sounds like a Zen-like hacker koan or something, but it is simply a list of directories (folders) that are searched automatically when an unknown command is typed in at the command prompt. Some commands, like 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.»)
Читайте также:  Linux ubuntu web камера

Extensions

  • Unlike the old DOS command prompts that a lot of people remember, you do not need an ‘extension’ (like .sh or .py or anything else), but it helps to keep track of things. It is really only there for humans to use as a reference and most command lines and programs will not care in the least. It won’t hurt. If the script name contains an extension, however, you must use it. It is part of the filename.

Changing directories

  • You do not need to be in any certain directory at all for any reason. But if the directory is not on the path (type 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.’

Typing the program name

  • You do not need to type out the name of the program that runs the file (BASH or Python or whatever) unless you want to. It won’t hurt, but there are a few times when you may get slightly different results.

SUDO

  • You do not need 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.

Script location .

# 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 

A: To «execute this script» from the terminal on a Unix/Linux type system, you have to do three things:

1. Tell the system the location of the script. (pick one)

# 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 $ # or > printf "%b" "$" /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin 

2. Tell the system that the script has permission to execute. (pick one)

# 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.

3. Tell the system the type of script. (pick one)

  • Type the name of the program before the script. (Note: when using this method, the execute(chmod thing above) is not required
> bash /path/to/script.sh . > php /path/to/script.php . > python3 /path/to/script.py . 
  • Use a shebang, which I see you have ( #!/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.
  • Use a «portable» shebang. You can also have the system choose the version of the program that is first in the 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 

Источник

Читайте также:  Web components plugin linux

How to Run a Shell Script in Linux [Essentials Explained for Beginners]

Here are all the essential details you should know about executing a shell script in the Linux command line.

That maybe simple, but it doesn’t explain a lot. Don’t worry, I’ll do the necessary explaining with examples so that you understand why a particular syntax is used in the given format while running a shell script. I am going to use this one line shell script to make things as uncomplicated as possible:

[email protected]:~/Scripts$ cat hello.sh echo "Hello World!"

Method 1: Running a shell script by passing the file as argument to shell

The first method involves passing the script file name as an argument to the shell. Considering that bash is the default shell, you can run a script like this:

Do you know the advantage of this approach? Your script doesn’t need to have the execute permission. Pretty handy for quick and simple tasks. Run A Shell Script LinuxIf you are not familiar already, I advise you to read my detailed guide on file permission in Linux. Keep in mind that it needs to be a shell script that you pass as argument. A shell script is composed of commands. If you use a normal text file, it will complain about incorrect commands. Running Text File As Script in LinuxIn this approach, you explicitly specified that you want to use bash as the interpreter for the script. Shell is just a program and bash is an implementation of that. There are other such shells program like ksh, zsh, etc. If you have other shells installed, you can use that as well instead of bash. For example, I installed zsh and used it to run the same script: Execute Shell Script With Zsh

Method 2: Execute shell script by specifying its path

The other method to run a shell script is by providing its path. But for that to be possible, your file must be executable. Otherwise, you’ll have “permission denied” error when you try to execute the script. So first you need to make sure that your script has the execute permission. You can use the chmod command to give yourself this permission like this:

Читайте также:  Linux who uses file

Once your script is executable, all you need to do is to type the file name along with its absolute or relative path. Most often you are in the same directory so you just use it like this:

Running Shell Script In Other Directory

If you are not in the same directory as your script, you can specify it the absolute or relative path to the script:

That ./ before the script is important (when you are in the same directory as the script)

Executing Shell Scripts Linux

Why can you not use the script name when you are in the same directory? That is because your Linux systems looks for the executables to run in a few selected directories that are specified in the PATH variable. Here’s the value of PATH variable for my system:

[email protected]:~$ echo $PATH /home/abhishek/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
  • /home/abhishek/.local/bin
  • /usr/local/sbin
  • /usr/local/bin
  • /usr/sbin
  • /usr/bin
  • /sbin
  • /bin
  • /usr/games
  • /usr/local/games
  • /snap/bin

The binaries or executable files for Linux commands like ls, cat etc are located in one of those directories. This is why you are able to run these commands from anywhere on your system just by using their names. See, the ls command is located in /usr/bin directory.

Locating Command Linux

When you specify the script WITHOUT the absolute or relative path, it cannot find it in the directories mentioned in the PATH variable.

Why most shell scripts contain #! /bin/bash at the beginning of the shell scripts?

Remember how I mentioned that shell is just a program and there are different implementations of shells.

When you use the #! /bin/bash, you are specifying that the script is to run with bash as interpreter. If you don’t do that and run a script in ./script.sh manner, it is usually run with whatever shell you are running.

Does it matter? It could. See, most of the shell syntax is common in all kind of shell but some might differ.

For example, the array behavior is different in bash and zsh shells. In zsh, the array index starts at 1 instead of 0.

Bash Vs Zsh

Using #! /bin/bash indicates that the script is bash shell script and should be run with bash as interpreter irrespective of the shell which is being used on the system. If you are using zsh specific syntax, you can indicate that it is zsh script by adding #! /bin/zsh as the first line of the script.

The space between #! /bin/bash doesn’t matter. You can also use #!/bin/bash.

Was it helpful?

I hope this article added to your Linux knowledge. If you still have questions or suggestions, please leave a comment.

Expert users can still nitpick this article about things I missed out. But the problem with such beginner topics is that it is not easy to find the right balance of information and avoid having too much or too few details.

If you are interested in learning bash script, we have an entire Bash Beginner Series on our sysadmin focused website Linux Handbook.

If you want, you may also purchase the ebook with additional exercises to support Linux Handbook.

Источник

Оцените статью
Adblock
detector