2

I have a shell script named remote_execution.sh. Is it possible that I just type remote_execution in any of the folders and the execution starts, just like gcc, vi or any such command does?

Thanks in advance.

2
  • surely this has to be a dupe? Commented Mar 9, 2012 at 6:34
  • This doesn't really deserve to be an answer, but you could make a directory especially for your scripts and add that directory to your $PATH. Google "add directory to $PATH" for info on that. Commented Mar 9, 2012 at 22:27

7 Answers 7

7

The following four steps should allow you to run remote_execution from anywhere in your filesystem:

  1. Rename the file to remote_execution, removing the .sh extension
  2. Add a "shebang" line to the top of the file

    #!/bin/bash
    
  3. Modify the permissions of the file so it is executable (see man chmod)

    chmod u+x remote_execution
    
  4. Move the file into a directory in your PATH so it "works in any folder". At a guess:

    mv remote_execution /usr/local/bin
    
Sign up to request clarification or add additional context in comments.

2 Comments

How do you know that this is a bash script? How do you know it is missing a shebang? That it isn't already executable? That it is not in the PATH? Shouldn't he need to do a sudo mv remote_execution /usr/local/bin to write to /usr/local/bin?
@userunknown It's called probability. Assumptions were made, and they were safe assumptions.
4

Another option is set an alias

alias  remote_execution="fullpath to  remote_execution.sh"

Comments

3

You could put that script in any folder depicted by $PATH

$ echo $PATH

[andreas@nyert test]$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/andreas/bin

rename your script to remote_execution and put #!/bin/sh in the first line. Also chmod it to make sure it's executable

$ chmod 755 remote_execution

Comments

1

add the directory in which remote_execution.sh is located to your $PATH variable. Also if you want it to be launched without the ending .sh, rename the script to remote_execution

Comments

1

You have 3 ways: you either copy the script in one of the directories found in PATH or create a symlink for the script there (ln -s) or add the current directory of the shell script to the PATH (export PATH=$PATH:dir).

To find out what PATH looks like do a echo $PATH.

Comments

1

Yes. You have two options, put the script somewhere on your path, or append the script's directory to your path in your ~/.profile.

Comments

0

Each file has it's own permission bits. You have to modify the permissions of the file so it is executable. So use command -

chmod +x remote_execution

Hoping you have added "shebang" line to the top of the file

#!/bin/bash

If you call the script with an explicit interpreter, like

bash remote_execution.sh
/bin/bash remote_execution.sh
dash remote_execution.sh
sh remote_execution.sh

your choosen interpreter is used, no matter what the shebang says, which is just a comment. Otherwise the kernel looks for the shebang and starts the program with the therein specified interpreter. If Shebang character is used then no matter what extension is !

Your shell is a program. It has variables, one of them is $ PATH

$PATH contains A colon-separated list of directories to search for commands

If you want to see it just type $ echo $PATH in terminal.

On my computer it shows

vikram@vikram-Studio-XPS-1645:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

This means if I type command like gcc or vim it searches in above directory list.

Now just copy your shell script to any of above folder to access it from anywhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.