Linux command line run script

This post and this website contains affiliate links. See my disclosure about affiliate links.

how to run a script (.sh) file in linux from command line

Scripts are an integral part of any Linux or Unix systems. It refers to a piece of non-compiled programming code that requires an interpreter at run time to perform a task. The scripts can use any of the interpreted language such as Linux shell, Perl, Python, Ruby, JavaScript or any of the other hundreds of interpreted computer languages.

In linux shell environment, the script file usually have the .sh extension and is also referred to as the sh files. Sometimes, the files have extension specific to the shell that it is intended to run in, such as .bash, .csh or .ksh.

Scripts or Script files are used for a variety of purposes in Linux. Some Linux commands themselves are coded as scripts where as many of the compiled commands have several wrappers written around them to provide more convenience features. In addition, you can code your own custom script to automate many small tasks that needs to performed routinely. Also, many of the software have installer scripts that need to be run manually to install and configure products.

Shell scripts are Linux/Unix specific scripts that are designed and coded to run exclusively using the shell scripting languages available in Linux, such as sh, bash, csh, ksh etc. We will look at how to run these scripts, exclusively shell scripts (.sh files) from the command line.

In order to run the script file, you will need the appropriate permissions to execute it. If the execution bit is not set, then you should change the permissions on the script files using the chmod command.

The script files by convention have a .sh extension, but it is not necessary to have .sh extension or any extension for that matter. You can make any file (with any or no extension) executable by changing the permissions, and as long as the file content has the appropriate code that can be interpreted and executed by the shell, it should work just fine.

Run Script from Command Line

You can run the script by just typing in the name of the script, but that effectively depends on the PATH environment variable that set on the command shell. In order to make it independent of the variable settings, you can provide the path to the script file. You can provide either the relative path or the absolute path.

Читайте также:  Linux ubuntu all version

The generic syntax looks like this….

An example of relative path

As mentioned, if the directory where the script file resides is in the PATH variable, then it is not necessary to provide the path to the file. You can just type in the script name and the shell will search all the directories in the path in order and execute the first one it finds.

A shell script can be executed in or using any of the supported shell environment in Linux. In a properly coded script, the first line or the shebang specifies exactly which shell to be used while executing the file. A Shebang or Shabang is usually the first line that looks something like…

The command line shell will fork a process/sub-shell with the specified value and execute the script in the sub-shell. This helps to keep the current shell clean. This is the ideal way to execute the script in most cases, such as when you are installing a product or executing a command.

But many times, you would want the script to execute in the command line shell without forking a sub-shell. This is especially useful, when the script modifies some environment values (such as path) that you want to continue to use even after the script exists.

Run Script using the Current Shell

In order to run the script in the current shell without forking another process you can use either the . operator (dot) or the source command.

They are mostly equivalent for most practical purposes, except for some minor differences depending on the shell you are using. The . (dot) operator mostly works in bash and its related shells while the source command will work in other shells as well, such as csh.

Another advantage when executing the script this way, either using source or the dot operator is that you don’t need the executable permissions for the file, just the read permission will suffice. This can be quite handy if you cannot modify the permissions for file for some reason.

Run Script using Another Shell

Sometimes, you will want to run the script using another shell rather than the one you are using in the command window. This is also one way to override the value that is set in the shebang of the script file.

You type the shell you want to execute followed by the script file name to execute the script. For example, to run the script named myscript.sh using the sh shell, you will use the following command

or to use bash or ksh, just type

bash$ bash /path/to/myscript.sh

bash$ ksh /path/to/myscript.sh

Источник

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

Читайте также:  Время выполнения операции linux

$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.»)

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 

Источник

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