Linux execute file command line

How do I run .sh scripts?

Whenever I open a .sh file, it opens it in gedit instead of the terminal. I can’t find any option similar to Right ClickOpen WithOther Application.Terminal. How do I open this file in the terminal?

You shouldn’t use extensions on scripts. At some point in the future, you may find that a different language is more suitable to do the task your current script is doing. And then you have a problem. Do you keep the old name, with a completely misleading extension, or do you rename it, possibly having to edit alot of places where your script is used?

You don’t need the file extension. It’s nice to have but is not needed. The OS doesn’t look at the file extension. It looks at the data

16 Answers 16

Give execute permission to your script:

chmod +x /path/to/yourscript.sh 

Since . refers to the current directory: if yourscript.sh is in the current directory, you can simplify this to:

Actually, you can use . /path/to/yourscript.sh if the script have to set up some environment variables.

Nobody mentions the traditional: ./path/to/yourscript.sh (without the space after . )? I find that one is the simplest and easiest to use. But anyways, here is my alternative that should do almost the same as ./ would, though I don’t see why you wouldn’t use ./ : (FILENAME=~/rem4space.sh;SLL=$(cat $FILENAME|head -1|sed ‘s:^#!\(.*\):\1:g’);[ ! -z $SLL ] && exec $SLL $FILENAME;sh $FILENAME) . edit FILENAME to your liking. Also note that sh will be used if there is no alternative.

You need to mark shell scripts as executable to run them from the file manager:

  1. Right click on your .sh file and select Properties: enter image description here
  2. In the Permissions tab, check Allow executing file as program: enter image description here
  3. Close the Properties window and double-click the file. A dialog will pop up giving you the option to run the script in a terminal: enter image description here

This isn’t working in Ubuntu 13.04. Keeps opening in gedit anyway, never asks me to execute. Edit: Nvm, imjustmatthew answers this.

Before using this we need to make the file permission for execute using chmod. chmod +x filename.sh or chmod 755 filename.sh

Читайте также:  Postgrespro 1c установка linux

Источник

linux — how to run/execute a file in the command line without the ./

Let’s say the file ‘foo.sh’ has all permissions and I just want to run the file in the current directory:

what/where could I change something in order to execute the file (any file with permissions) just by typing the name:

The idea would be that I could ALWAYS do this way (in the current directory). In case ./file.sh is a valid way to execute a file, file.sh would work too. Not intended to run the file.sh globally.

You should know that there is a reason for the default behavior. It is safe not to run such things from the current directory, because then anyone could put a malicious file like ‘cd’ to any of your $HOME folder, and it will be called instead of original command. unix.stackexchange.com/questions/65700/…

5 Answers 5

If you want to execute this file specific to local user then create ~/bin directory put the file inside of it and append this path to your $PATH env. ‘:’ is a separator Check following link.

But if you want to access this globally. Then place your file in /usr/bin or /usr/local/bin and check are these paths already registered in $PATH then. Add your permissions. open new terminal try to call file.

You can also copy the script under /usr/local/bin directory which is for user programs.

Well, shouldn’t that more properly be /usr/local/bin if you are going to start randomly copying to the filesystem?

Edit your «$PATH» variable to include the directory in which this file is located. For example, this is a part of my «$PATH» : /usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/rany/bin

It includes the following directories:

  • /usr/local/bin
  • /usr/bin
  • /bin
  • /usr/local/sbin
  • /usr/sbin
  • /sbin
  • /home/rany/bin

After putting an executable script in one of these directories you’ll be able to execute it by simply typing its name at the command line.

After doing so, I suggest you to read this Bash FAQ for extra information.

Источник

How to run an .exe from linux command prompt

but if it’s really a Windows program, you will need to install «wine», then do:

but only some Windows programs will work under wine.

This anwswer is combining other answers in to this question into one.

The info.exe file will either execute under Linux or Windows, but not both.

Executes Under Windows

If the file is a windows file, it will not run under Linux on it’s own. So if that’s the case, you could try running it under a windows emulator (WINE). If it’s not compatible with wine, then you won’t be able to execute it under Linux.

Читайте также:  Linux write permissions to file

Before you can start, you will need to install wine. The steps you need to install wine will vary on the linux platform you are on. You can probably google «Ubuntu install wine», if for example, you’re installing ubuntu.

Once you have wine installed, then you’d be able to execute these commands.

Execute Under Linux

if you know this file to run under linux, then you’ll want to execute these commands:

Change to your abc directory

Then you’ll want to change permissions to allow all users to execute this file (a+x).
you could also allow just the user to execute (u+x)

Launch the program, the ./ tells the command line to look in the current path for the file to execute (if the ‘current’ directory isn’t in the $PATH environment variable.

«you could try running it under a windows emulator (WINE).» WINE does stand for «Wine Is Not an Emulator».

My comment was meant to be both a joke a a proposition for an edit. AFAIK Wine basically replaces windows calls for POSIX equivalents, so whoever named it was not ironic, that is WINE is indeed not an emulator.

you can’t 🙂 exe is Microsoft only. now if you had a linux executable you could do:

cd folder chmod +x file ./file 

This is false in general. Many Windows and DOS EXEs can be run on Linux using emulators such as wine or dosbox .

@Royi probably «yes», but that depends on how simple we’re talking. But wine should also do your trick.

On Linux you give the file executable permissions. It isn’t the extension that determines whether or not it can be executed (as on windows.)

Assuming you have a valid file that can be executed in Linux, (not a windows/dos file) do this:

cd abc chmod a+x info.exe ./info.exe 

Note that you need the leading ./ for the shell to find the file in the current directory!

This really belongs on superuser though.

The first line changes the directory, the second line tells linux it is executable, the third runs the program.

I recently wanted to run some old MSDOS .exe files and I could just use dosbox. On Ubuntu it was just

Читайте также:  Linux флешка сохранение изменений

.exes are generally Windows executables, not linux ones. To run those, use something like WINE.

Otherwise, to run a Linux executable, there are many ways, e.g.:

  1. cd abc; ./info.exe
  2. ./abc/info.exe
  3. /full/path/to/abc/info.exe
  4. Add «abc» to your PATH, then just run it as a normal command.

I struggled so much until I found this website and used the ‘terminal’ tips section at the bottom of the page: winehq.org/download/ubuntu

Wine is a program that you can install, which allows you to run .exe files on linux.

(go to the directory of your file: /cd (ex: Desktop/) And to open your .exe file:

If you have any problems with wine, you can do wine —help .

Here is how to run an executable file in Linux:

  1. open terminal with ctrl + alt + T : sudo apt-get update
  2. install Wine: sudo apt-get install wine
  3. go to the directory in which your .exe file is placed by changing directory: cd /Desktop
  4. wine filename.exe

Hit enter and your .exe file will be executed.

Executing a Linux executable on Linux

If the executable is a Linux executable, you need to make sure that your shell can find it. Here are some ways how to do that.

But first, make sure it’s executable. You can check whether the x (executable) flag is set using ls -l abc/info.exe and you can set it with chmod +x abc/info.exe .

  • Run it with relative path, in your example: abc/info.exe .
  • Run it with absolute path, for example: /home/username/abc/info.exe (depends on where it actually is)
  • Place the binary in a directory that is part of the PATH that is searched by the shell to find binaries. For example, cp abc/info.exe ~/bin/ . If ~/bin is part of PATH , you can now run info.exe without qualifying it.
  • Make the directory that contains the binary part of the PATH , for example, export PATH=~/abc:$PATH . Note that this is for the current shell only, unless you add this line to your .bashrc or .profile

Executing a Windows executable on Linux

If the executable is a Windows executable, you need to install wine . Then you can run it using wine abc/info.exe . If you want to run it like a Linux program, you need to install wine-binfmt . Then you can run it the same way as described above for Linux executables.

If you use Ubuntu, install wine like this:

sudo apt-get install wine wine-binfmt 

Источник

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